Skip to content

Commit

Permalink
added test for newtemp().
Browse files Browse the repository at this point in the history
  • Loading branch information
johngunderman committed Nov 5, 2009
1 parent b05784f commit 9b9856d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -5,7 +5,7 @@ objects = test_suite.o \
intmdt_code_gen.o
name = linked_lists

CFLAGS = -Wall -Wextra -Werror -pedantic -O -g -std=c99
CFLAGS = -g -Wall -Wextra -Werror -pedantic -O -g -std=c99

#default : $(objects)
# gcc -o $(name) $(objects)
Expand Down
15 changes: 10 additions & 5 deletions intmdt_code_gen.c
Expand Up @@ -48,26 +48,31 @@ env_t *init_env(hash_table_t *table) {

/*
Creates a new temporary variable and inserts it in the
symbol table. Temp vars begin with @. Exits program on failure.
symbol table. Temp vars begin with @. Returns NULL on failure.
*/
intmdt_addr_t *newtemp(env_t *top) {
static int temp_num = 0; /* the id for the new temp */

if (top == NULL || top->table == NULL) {
fprintf(stderr, "Attempted to call newtemp with null environment.\n");
return NULL;
}

temp_num++;

char *key = malloc (sizeof(char) * 15); /* we assume we shouldn't need
more than 15 characters. */

if (key == NULL) {
fprintf(stderr, "Failed to malloc key in newtemp()\n");
exit(1);
return NULL;
}

id_type_t *value = malloc (sizeof(id_type_t));

if (value == NULL) {
fprintf(stderr, "Failed to malloc value in newtemp()\n");
exit(1);
return NULL;
}

value->type = &unknown_var; /* set to 8 for unknown type */
Expand All @@ -77,14 +82,14 @@ intmdt_addr_t *newtemp(env_t *top) {
value->supersize = NULL;

sprintf(key, "@%d", temp_num);

list_entry_t *entry = hash_table_insert (top->table, key, value);

intmdt_addr_t *addr = malloc ( sizeof(intmdt_addr_t));

if (addr == NULL) {
fprintf(stderr, "Failed to malloc intmdt_addr_t in newtemp()\n");
exit(1);
return NULL;
}

addr->type = symbol;
Expand Down
16 changes: 13 additions & 3 deletions test_suite.c
Expand Up @@ -226,9 +226,17 @@ void sizeofidtype_test () {
}

void newtemp_test() {
env_t *env = init_env();
intmdt_addr_t *t1 = newtemp(env);
intmdt_addr_t *t2 = newtemp(env);
env_t *env = init_env(hash_table_init(cmp_string, string_hasher));

newtemp(env);
printf("Testing newtemp():\n");

assert(hash_table_search(env->table, "@1") != NULL);
printf("Passed first test\n");

intmdt_addr_t *t2 = newtemp(NULL);
assert(t2 == NULL);
printf("Passed the second test (env = NULL)\n");

}

Expand All @@ -255,6 +263,8 @@ int main () {
printf ("\n");
sizeofidtype_test();
printf ("\n");
newtemp_test();
printf ("\n");
return 0;
}

Expand Down

0 comments on commit 9b9856d

Please sign in to comment.