+/EE312
software design
git clone https://git.julia.blue/+/EE312
796bbdc
master
1// StringADT demonstration file main.cpp 2#include <stdio.h> 3#include <string.h> // declares the built-in C string library functions, like strcat 4#include "String.h" // declares our new improved String functions, like utstrcat 5#include <stdlib.h> 6 7void testStage1 (void ) { 8char p [12 ]; 9const char * q = "Hello World" ; 10UTString * s ; 11UTString * t ; 12 13printf ("this test should print Hello World three times\n" ); 14 15for (unsigned k = 0 ;k < 12 ;k += 1 ) { 16p [k ]= q [k ]; 17 } 18s = utstrdup (p ); 19printf ("%s\n" ,s -> string ); 20 21q = "you goofed!" ; 22for (unsigned k = 0 ;k < 12 ;k += 1 ) { 23p [k ]= q [k ]; 24 } 25printf ("%s\n" ,s -> string ); 26 27t = utstrdup (s -> string ); 28utstrfree (s ); 29printf ("%s\n" ,t -> string ); 30utstrfree (t ); 31} 32 33void testStage2 (void ) { 34char c_str1 [20 ]= "hello" ; 35UTString * ut_str1 ; 36UTString * ut_str2 ; 37 38printf ("Starting stage 2 tests\n" ); 39strcat (c_str1 ," world" ); 40printf ("%s\n" ,c_str1 );// nothing exciting, prints "hello world" 41 42ut_str1 = utstrdup ("hello " ); 43ut_str1 = utstrrealloc (ut_str1 ,20 ); 44 45utstrcat (ut_str1 ,c_str1 ); 46printf ("%s\n" , 47ut_str1 -> string );// slightly more exciting, prints "hello hello world" 48utstrcat (ut_str1 ," world" ); 49printf ("%s\n" , 50ut_str1 -> string );// exciting, should print "hello hello world wo", 'cause there's not enough room for the second world 51 52ut_str2 = utstrdup ("" ); 53ut_str2 = utstrrealloc (ut_str2 ,11 ); 54utstrcpy (ut_str2 ,ut_str1 -> string + 6 ); 55printf ("%s\n" ,ut_str2 -> string );// back to "hello world" 56 57ut_str2 = utstrrealloc (ut_str2 ,23 ); 58utstrcat (ut_str2 ," " ); 59utstrcat (ut_str2 ,ut_str1 -> string ); 60printf ("%s\n" ,ut_str2 -> string );// now should be "hello world hello hello" 61 62utstrfree (ut_str1 ); 63utstrfree (ut_str2 ); 64} 65 66#define BIG 1000000 67void testStage3 (void ) { 68int k ; 69UTString * ut_str1 = utstrdup ("" ); 70ut_str1 = utstrrealloc (ut_str1 ,BIG );// big, big string 71 72printf ("attempting stage 3 test. This shouldn't take long...\n" ); 73printf ("(no really, it shouldn't take long, if it does, you fail this test)\n" ); 74fflush (stdout ); 75 76for (k = 0 ;k < BIG ;k += 1 ) { 77utstrcat (ut_str1 ,"*" ); 78 } 79if (ut_str1 -> string [BIG - 1 ]!= '*' ) { 80printf ("stage3 fails for not copying all the characters\n" ); 81 }else if (strlen (ut_str1 -> string )!= BIG ) { 82printf ("Hmmm, stage3 has something wrong\n" ); 83 }else { 84printf ("grats, stage 3 passed (unless it took a long time to print this message)\n" ); 85 } 86utstrfree (ut_str1 ); 87} 88 89#define BAD_SIGNATURE (~0xbaadbeef) 90 91/* 92* This is an important test that likely sounds strange: 93* Each of the following lines should crash the program by failling an assert. 94* Try each, one at a time, to make sure your program acts as expected. 95* If your program does not crash, you are doing something wrong and will lose points. 96*/ 97void testStage4 (void ) { 98char p [20 ]= "Hello World!" ; 99UTString * utstr1 = utstrdup ("Hello World" ); 100* (uint32_t * )(utstr1 -> string + utstr1 -> length + 1 )= BAD_SIGNATURE ; 101// printf("crashing with utstrlen\n\n\n"); utstrlen(utstr1); 102// printf("Crashing with utstrcpy\n\n\n"); utstrcpy(utstr1, p); 103// printf("crashing with utstrcat\n\n\n"); utstrcat(utstr1, p); 104// printf("crashing with utstrfree\n\n\n"); utstrfree(utstr1); 105// printf("crashing with utstrrealloc\n\n\n"); utstrrealloc(utstr1, 40); 106} 107 108void custom1 (void ){ 109char empty []= "" ; 110UTString * utstr1 = utstrdup ("" ); 111utstrcat (utstr1 ,empty ); 112utstrcpy (utstr1 ,empty ); 113utstrrealloc (utstr1 ,20 ); 114utstrcat (utstr1 ,empty ); 115char notEmpty []= "this one isn't empty and is long" ; 116utstrcat (utstr1 ,notEmpty ); 117printf ("%s\n" ,utstr1 -> string ); 118printf ("Custom test 1 passed\n" ); 119} 120 121int main (void ) { 122testStage1 (); 123testStage2 (); 124testStage3 (); 125testStage4 (); 126custom1 (); 127return 0 ; 128}