#include "shared.h" #include "env.h" #include "list.h" #include "intcode.h" #include "mem.h" #include "list.h" #include "cmp.h" #include "quad.h" #include #include #include //TESTS GO HERE #define HW 5 void testGen() { printf("-----starting gen tests---------\n"); intmdt_code_t *test_code = (intmdt_code_t*)malloc(sizeof(intmdt_code_t)); env_t *cur_env = env_init(); /*first test case: x = 1 + 3*/ /*--------------------------*/ intmdt_addr_t *x = (intmdt_addr_t*)(malloc(sizeof(intmdt_addr_t))); x->type = symbol; list_entry_t *x_entry = (list_entry_t*)malloc(sizeof(list_entry_t)); x_entry->key = "x"; id_type_t *x_type = (id_type_t*)malloc(sizeof(id_type_t)); x_type->type = int_var; x_type->dimension = 0; x_type->size = 0; x_type->subtype = NULL; x_type->supertype = NULL; x_entry->value = x_type; x->type = symbol; x->addr.entry_ptr = x_entry; intmdt_addr_t *one = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); int one_i = 1; one->type = int_const; one->addr.int_const_ptr = &one_i; intmdt_addr_t *three= (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); int three_i = 3; three->type = int_const; three->addr.int_const_ptr = &three_i; gen(test_code,cur_env,"+",one,three,x); /* end first case*/ /*second case: q = (1 + 3) * 3.2*/ //uses the first test result as (1+3) intmdt_addr_t *q = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); list_entry_t *q_entry = (list_entry_t*)malloc(sizeof(list_entry_t)); q_entry->key = "q"; q->type = symbol; id_type_t *q_type = (id_type_t*)malloc(sizeof(id_type_t)); q_type->type = float_var; q_type->dimension = 0; q_type->size = 0; q_type->subtype = NULL; q_type->supertype =NULL; q_entry->value = q_type; q->type = symbol; q->addr.entry_ptr = q_entry; intmdt_addr_t *threetwo = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); threetwo->type = float_const; double thtw_d = 3.2; threetwo->addr.double_const_ptr = &thtw_d; int pass = (gen(test_code, cur_env, "*",x,threetwo,q) != NULL); printf("pass: %d, should be 1(success)\n",pass); /* end test 2*/ /* test 3: r =3.2 / 4 */ intmdt_addr_t *r = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); r->type = symbol; list_entry_t *r_ent = (list_entry_t*)malloc(sizeof(list_entry_t)); r_ent->key = "r"; id_type_t *r_type = (id_type_t*)malloc(sizeof(id_type_t)); r_type->type = float_var; r_type->size = 0; r_type->dimension = 0; r_type->subtype = NULL; r_type->supertype = NULL; r_ent->value = r_type; r->addr.entry_ptr = r_ent; intmdt_addr_t *four = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); four->type = int_const; int four_int = 4; four->addr.int_const_ptr = &four_int; gen(test_code, cur_env, "/", threetwo, four, r); /*end test 3*/ /*4th test case: assumed failure*/ //int v = 3.2 + x intmdt_addr_t *v = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); v->type = symbol; list_entry_t *v_ent = (list_entry_t*)malloc(sizeof(list_entry_t)); v_ent->key = "v"; id_type_t *v_type = (id_type_t*)malloc(sizeof(id_type_t)); v_type->type = int_var; v_type->size = 0; v_type->subtype = NULL; v_type->supertype = NULL; v_ent->value = v_type; v->addr.entry_ptr = v_ent; printf("Test 4 : int = float + int, should fail on type check\n"); int fail = (gen(test_code, cur_env,"+",threetwo,x,v) != NULL); printf("fail test: %d should be 0",fail); printf("\n"); printCodeList(test_code); printf("-----------end gen tests---------\n"); free(x); free(x_entry); free(x_type); free(q); free(q_entry); free(q_type); free(one); free(three); free(threetwo); free(four); free(r); free(v); free(r_ent); free(r_type); free(code_list); } void test_temp() { env_t *test = env_init(); printf("testing temp\n"); intmdt_code_t *test_code = (intmdt_code_t*)malloc(sizeof(intmdt_code_t)); intmdt_addr_t *addr = newtemp(test); printf("should be #0: "); printaddr2(addr,test_code); printf("\n"); /*test that the code handles failures properly. IE. it returns null*/ intmdt_addr_t *null_addr = newtemp(NULL); assert(null_addr == NULL); printf("successfully handled null environ\n"); env_delete(test); free(test_code); free(addr); free(null_addr); } /*testing widen. Right now, this is just making sure I can run the code without segfaulting- it doesn't actually check the value*/ void test_widen() { printf("-------starting widen tests----------\n"); env_t *test = env_init(); intmdt_code_t *test_code = (intmdt_code_t*)malloc(sizeof(intmdt_code_t)); id_type_t *id = (id_type_t*)malloc(sizeof(id_type_t)); id->type = float_var; id->dimension = 0; id->size = 0; id->subtype = NULL; id->supertype = NULL; intmdt_addr_t *orig = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); orig->type = int_const; int *const_ptr = (int*) malloc(sizeof(int)); int val = 3; const_ptr = &val; orig->addr.int_const_ptr = const_ptr; printf("starting widen\n"); widen(test_code, test, orig, id); printf("good widen complete\n"); id_type_t *id_int = (id_type_t*)malloc(sizeof(id_type_t)); id->type = int_var; id->dimension = 0; id->size = 0; id->subtype = NULL; id->supertype = NULL; intmdt_addr_t *orig2 = (intmdt_addr_t*)malloc(sizeof(intmdt_addr_t)); orig2->type = float_const; double f = 3.5; double *float_c = &f; orig2->addr.double_const_ptr = float_c; printf("starting widen\n"); widen(test_code,test,orig2,id_int); printf("failing test complete\n"); free(test); free(test_code); free(orig); } /*this is the test method for sizeofidtype*/ void test_sizeofidtype() { printf("-------starting sizeofidtype tests----------\n"); id_type_t* ids = calloc(4,sizeof(id_type_t)); ids[0].type = int_var; int_val = 0; ids[0].size = int_val; ids[0].dimension = 0; ids[0].subtype = NULL; ids[0].supertype = NULL; printf("sizeof {&int_var,0,0,NULL,NULL} = %u, should be %lu\n",sizeofidtype(&ids[0]),sizeof(int)); ids[1].type = float_var; ids[1].size = int_val; ids[1].dimension = 0; ids[1].subtype = NULL; ids[1].supertype = NULL; printf("sizeof {&float_var,0,0,NULL,NULL} = %u",sizeofidtype(&ids[1])); printf(" should be %lu\n",sizeof(double)); int arsize1 = 10; ids[2] = *((id_type_t*)makeArray(&ids[1],&arsize1)); printf("size of {&float_var,1,10,NULL,NULL} = %u", sizeofidtype(&ids[2])); printf(" should be %lu\n", 10* sizeof(double)); int arsize2 = 50; ids[3] = *((id_type_t*)makeArray(&ids[2],&arsize2)); printf("sizeof {&float_var, 2, 50,&{&float_var, 1, 10, NULL, &supertype}, NULL} = %u", sizeofidtype(&ids[3])); printf(" should be %lu\n", 500 * sizeof(double)); free(ids); printf("---------finish sizeofidtype tests--------\n"); } //come from hw3.y extern void* setID2(void*, void*, env_t*); void test_env() { printf("----------testing env-----------\n"); env_t *test = env_init(); //test1: just add variables,only 1 layer setID2(makeType(int_var),"a",test); int size = 20; setID2(makeArray(makeType(float_var),&size),"x",test); //print_env(test); //test2: multiple levels, no overlap env_t *test2 = env_scope_init(test); setID2(makeType(int_var),"b",test2); setID2(makeType(float_var),"abcd",test2); // print_env(test2); //test3: multiple levels, redefine type env_t *test3 = env_scope_init(test2); setID2(makeType(int_var),"x",test3); //test4: get an error on a second declaration of a variable within a scope printf("this should give an error:\n"); setID2(makeType(float_var),"x",test); printf("\n"); print_env(test3); env_delete(test3); } void print_list(list_head_t *list) { printf("{\n"); list_entry_t *entry = list->list; while(entry != NULL) { printf(" %s\n",entry->key); entry = entry->next; } printf("}\n"); } void test_listmerge() { printf("-----testing list_merge-----\n"); list_head_t *list = list_init(cmp_string); list_head_t *list2 = list_init(cmp_string); list_insert(list, "a", NULL); list_insert(list,"b",NULL); list_insert(list2,"c",NULL); list_insert(list2,"d",NULL); list_merge(list,list2); print_list(list); //testing null argument list_merge(list, NULL); list_delete(list); free(list2); } void test_backpatch() { printf("-------testing backpatch--------\n"); intmdt_code_t *code = malloc(sizeof(intmdt_code_t)); quadruple_t *goto1 = gen2(code,NULL,NULL,NULL,NULL); list_head_t *back_list = list_makelist(goto1); env_t *cur = env_init(); int i = 3; int j = 5; intmdt_var_t *num3 = makeNum(&i); intmdt_var_t *num5 = makeNum(&j); intmdt_var_t *eq1 = makeOp(num3, "+",num5,code,cur); backpatch(back_list,eq1->var.quad_ptr); int k = 100; quadruple_t *goto2 = gen2(code, ">", ((intmdt_var_t*)makeNum(&k))->var.addr_ptr,eq1->var.quad_ptr->result,NULL); back_list = list_makelist(goto2); backpatch(back_list,code->code); printCodeList(code); //test handeling of NULL printf("error on null: %d\n",backpatch(back_list, NULL)); } extern void *makeArray(void*, void*); extern void* makeType(void*); void testActivationRecordSize() { printf("-------testing activation record size-----\n"); env_t *env = env_init(); int five = 5; int seven = 7; int *five_p = malloc(sizeof(int)); *five_p = five; int *seven_p = malloc(sizeof(int)); *seven_p = seven; id_type_t *i1 = makeArray(makeArray(makeType(float_var),five_p),seven_p); env_insert(env,"i",i1); id_type_t *j1 = makeType(int_var); env_insert(env,"j",j1); printf("size is %d, should be %d\n",sizeof_record(env),4*3 + 8*35 + 4); env_t *env2 = env_scope_init(env); id_type_t *i2 = makeType(int_var); int three = 3; id_type_t *j2 = makeArray(makeArray(makeType(float_var),&three),&three); env_insert(env2, "i",i2); env_insert(env2,"j",j2); printf("size is %d, should be %d\n",sizeof_record(env2),4*3+8*9+4); printf("locations of variables in second test program\n"); // printf("loc is the last element in the list of stuff printed for each entry:\n"); // print_env(env2); printf("locations:\n"); printf(" loc of i1: %d\n",locof_record(env,"i")); printf(" loc of j1: %d\n",locof_record(env,"j")); printf(" loc of i2: %d\n",locof_record(env2,"i")); printf(" loc of j2: %d\n",locof_record(env2,"j")); env_delete(env2); printf("starting my test:\n"); env = env_init(); env_insert(env,"x",makeType(int_var)); env_insert(env,"y",makeType(float_var)); int *twenty = malloc(sizeof(int)); *twenty = 20; env_insert(env,"z",makeArray(makeType(float_var),twenty)); printf("location of x: %d\n",locof_record(env,"x")); printf("location of y: %d\n",locof_record(env,"y")); printf("location of z: %d\n",locof_record(env,"z")); printf("size of env: %d\n",sizeof_record(env)); } void run_tests() { if(HW == 4) { testGen(); test_temp(); test_widen(); test_sizeofidtype(); } else if(HW == 5) { test_env(); test_listmerge(); test_backpatch(); testActivationRecordSize(); } }