+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanFix memory leak796bbdc

master
3.8 KiB128 linesraw
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) {
8    char p[12];
9    const char* q = "Hello World";
10    UTString* s;
11    UTString* t;
12
13    printf("this test should print Hello World three times\n");
14
15    for (unsigned k = 0; k < 12; k += 1) {
16        p[k] = q[k];
17    }
18    s = utstrdup(p);
19    printf("%s\n", s->string);
20
21    q = "you goofed!";
22    for (unsigned k = 0; k < 12; k += 1) {
23        p[k] = q[k];
24    }
25    printf("%s\n", s->string);
26
27    t = utstrdup(s->string);
28    utstrfree(s);
29    printf("%s\n", t->string);
30    utstrfree(t);
31}
32
33void testStage2(void) {
34    char c_str1[20] = "hello";
35    UTString* ut_str1;
36    UTString* ut_str2;
37
38    printf("Starting stage 2 tests\n");
39    strcat(c_str1, " world");
40    printf("%s\n", c_str1); // nothing exciting, prints "hello world"
41
42    ut_str1 = utstrdup("hello ");
43    ut_str1 = utstrrealloc(ut_str1, 20);
44
45    utstrcat(ut_str1, c_str1);
46    printf("%s\n",
47           ut_str1->string); // slightly more exciting, prints "hello hello world"
48    utstrcat(ut_str1, " world");
49    printf("%s\n",
50           ut_str1->string); // exciting, should print "hello hello world wo", 'cause there's not enough room for the second world
51
52    ut_str2 = utstrdup("");
53    ut_str2 = utstrrealloc(ut_str2, 11);
54    utstrcpy(ut_str2, ut_str1->string + 6);
55    printf("%s\n", ut_str2->string); // back to "hello world"
56
57    ut_str2 = utstrrealloc(ut_str2, 23);
58    utstrcat(ut_str2, " ");
59    utstrcat(ut_str2, ut_str1->string);
60    printf("%s\n", ut_str2->string); // now should be "hello world hello hello"
61
62    utstrfree(ut_str1);
63    utstrfree(ut_str2);
64}
65
66#define BIG 1000000
67void testStage3(void) {
68    int k;
69    UTString* ut_str1 = utstrdup("");
70    ut_str1 = utstrrealloc(ut_str1, BIG); // big, big string
71
72    printf("attempting stage 3 test. This shouldn't take long...\n");
73    printf("(no really, it shouldn't take long, if it does, you fail this test)\n");
74    fflush(stdout);
75
76    for (k = 0; k < BIG; k += 1) {
77        utstrcat(ut_str1, "*");
78    }
79    if (ut_str1->string[BIG - 1] != '*') {
80        printf("stage3 fails for not copying all the characters\n");
81    } else if (strlen(ut_str1->string) != BIG) {
82        printf("Hmmm, stage3 has something wrong\n");
83    } else {
84        printf("grats, stage 3 passed (unless it took a long time to print this message)\n");
85    }
86    utstrfree(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) {
98    char p[20] = "Hello World!";
99    UTString* 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){
109    char empty[] = "";
110    UTString* utstr1 = utstrdup("");
111    utstrcat(utstr1, empty);
112    utstrcpy(utstr1, empty);
113    utstrrealloc(utstr1, 20);
114    utstrcat(utstr1, empty);
115    char notEmpty[] = "this one isn't empty and is long";
116    utstrcat(utstr1, notEmpty);
117    printf("%s\n", utstr1->string);
118    printf("Custom test 1 passed\n");
119}
120
121int main(void) {
122    testStage1();
123    testStage2();
124    testStage3();
125    testStage4();
126    custom1();
127    return 0;
128}