From 9b9856d5b23fa57402948ce9fc18a1a853a2b560 Mon Sep 17 00:00:00 2001 From: John Gunderman Date: Thu, 5 Nov 2009 11:08:59 -0500 Subject: [PATCH] added test for newtemp(). --- Makefile | 2 +- intmdt_code_gen.c | 15 ++++++++++----- test_suite.c | 16 +++++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 61637f2..a511302 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/intmdt_code_gen.c b/intmdt_code_gen.c index bdb0373..27e5e0e 100644 --- a/intmdt_code_gen.c +++ b/intmdt_code_gen.c @@ -48,11 +48,16 @@ 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 @@ -60,14 +65,14 @@ intmdt_addr_t *newtemp(env_t *top) { 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 */ @@ -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; diff --git a/test_suite.c b/test_suite.c index d086490..87b0c65 100644 --- a/test_suite.c +++ b/test_suite.c @@ -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"); } @@ -255,6 +263,8 @@ int main () { printf ("\n"); sizeofidtype_test(); printf ("\n"); + newtemp_test(); + printf ("\n"); return 0; }