/* tstring1.c */ /* test driver for module string1.c */ #include "string1.h" #include void write_string(String, char*); void test_at(String, int); void test_cat(String*, String*); main () { String S, S1, S2, S3, *SP; FILE *fout, *fin; char fname[10]; fprintf(stdout, "\t\tTest for functions StringInit and StringClr\n\n"); StringInit(&S1, "Test"); StringInit(&S2, "s"); StringInit(&S3, ""); write_string(S1, "S1"); write_string(S2, "S2"); write_string(S3, "S3"); StringClr(&S1); StringClr(&S2); StringClr(&S3); fprintf(stdout, "After function StringClr:\n"); write_string(S1, "S1"); write_string(S2, "S2"); write_string(S3, "S3"); fprintf(stdout, "\n\t\tTest for function StringAt\n\n"); StringInit(&S, "structure"); write_string(S, "S"); test_at(S, 0); test_at(S, 5); test_at(S, 8); test_at(S, 9); test_at(S, 10); StringClr(&S); StringInit(&S, ""); write_string(S, "S"); test_at(S, 0); test_at(S, 1); fprintf(stdout, "\n\t\tTest for function StringCpy\n\n"); fprintf(stdout, "- for non-empty string\n"); StringInit(&S1, "Data Structures"); write_string(S1, "S1"); SP = StringCpy(&S2, S1); fprintf(stdout, "After function StringCpy:\n"); write_string(S2, "S2"); write_string(*SP, "SP"); fprintf(stdout, "\n- for empty string\n"); StringInit(&S1, ""); write_string(S1, "S1"); SP = StringCpy(&S2, S1); write_string(S2, "S2"); write_string(*SP, "SP"); fprintf(stdout, "\n\t\tTest for function StringCat\n\n"); fprintf(stdout, "- when both strings are non-empty\n"); StringInit(&S1, "Better"); StringInit(&S2, "String"); test_cat(&S1, &S2); fprintf(stdout, "- when one of strings is empty\n"); StringClr(&S1); test_cat(&S1, &S2); StringClr(&S1); test_cat(&S2, &S1); fprintf(stdout, "- when both strings are empty\n"); StringClr(&S1); StringClr(&S2); test_cat(&S1, &S2); fprintf(stdout, "\t\tTest for function StringFPut\n\n" "Please enter the name of file you want to send " "output to for this test:\n"); fscanf(stdin, "%s", fname); fout = fopen (fname, "w"); assert(fout); fprintf(fout, "This is a test for function StringFPut.\n\n"); fprintf(fout, "The next line is empty\n"); StringFPut(S1, fout); fputc('\n', fout); fprintf(fout, "The next line is: \"Still testing...\"\n"); StringInit(&S1, "Still testing..."); StringFPut(S1, fout); fputc('\n', fout); fclose(fout); fprintf(stdout, "Now check the file '%s'\n", fname); fprintf(stdout, "\n\t\tTest for function StringFGet\n\n"); fin = fopen(fname, "r"); fprintf(stdout, "Content of file we just created:\n\t\t***\n"); while (StringFGet(&S1, fin)) { StringPut(S1); fputc('\n', stdout); } fprintf(stdout, "\t\t***\n"); fclose(fin); return 0; } /* prints out string s and its length */ void write_string(String s, char *name) { fprintf(stdout, "String %s : \"", name); StringPut(s); fprintf(stdout, "\" (%d)\n", StringLen(s)); } /* test for function StringAt */ void test_at(String s, int n) { char *c; c = StringAt(s, n); if (c) fprintf(stdout, "%dth element: %c\n",n, *c); else fprintf(stdout, "%d is outside the length\n", n); } /* test for function StringCat */ void test_cat(String *s1, String *s2) { assert(s1); assert(s2); write_string(*s1, "S1"); write_string(*s2, "S2"); StringCat(s1, *s2); write_string(*s1, "S1 after StringCat"); fputc('\n', stdout); }