void test_PCM_array_delete(void) { int i, e; int error = 0; PCMarray* mylist = PCMarraynew("list2"); printf ("insert 3 elements\n"); CALL(PCMarrayinsert (mylist, 0, 8)); CALL(PCMarrayinsert (mylist, 0, 100)); CALL(PCMarrayinsert (mylist, 0, 1000)); PCMarrayoutput (mylist); printf ("delete 2th element\n"); CALL(PCMarraydelete (mylist, 2, &e)); printf ("The 2th element %d was deleted\n", e); CALL(PCMarrayoutput (mylist)); CALL(PCMarraysort (mylist, mylist->length, 0)); CALL(PCMarrayoutput (mylist)); CALL(PCMarrayclear (mylist)); CALL(PCMarrayoutput (mylist)); CALL(PCMarrayfree (mylist)); mylist = NULL; if ( mylist == NULL ) printf("sqlist free successfully!\n"); TERMINATE: PCMcheckerror (error); if ( NULL != mylist ) PCMarrayfree (mylist); }
void test_PCM_array_merge(void) { int i; int error = 0; int a[5] = {1, 3, 3, 7, 7}; int b[5] = {1, 3, 6, 6, 10}; PCMarray* odd = PCMarraynew("list5"); PCMarray* even = PCMarraynew("list6"); CALL(PCMarraycopy (odd, a, 5)); CALL(PCMarraycopy (even, b, 5)); CALL(PCMarraymerge (odd, even)); CALL(PCMarraydeleteR (odd)); CALL(PCMarrayoutput (odd)); CALL(PCMarrayoutput (even)); CALL(PCMarrayfree (odd)); CALL(PCMarrayfree (even)); odd = even = NULL; if ( even == NULL ) printf ("free even successfully!\n"); if ( odd == NULL ) printf ("free odd successfully!\n"); TERMINATE: PCMcheckerror (error); }
int main(void) { int i; int error = 0; CALL(testoneelement()); TERMINATE: PCMcheckerror (error); return 0; }
void test_PCM_array_inout(void) { int error = 0; int num = 0; int max = 0; int count = 0; int sum = 0; PCMarray* list = PCMarraynew("list3"); printf("Please input intergers:\n"); while (1 == scanf ("%d", &num)) { CALL(PCMarrayinsert(list, count, num)); sum += num; if ( 0 == count ) { max = num; } else { if ( max < num ) { max = num; } } count++; } CALL(PCMarraysort (list, list->length, PCMALGSORTSELECT)); //CALL(PCMarraysort (list, list->length, PCMALGSORTBUBBLE)); //if ( error ) goto TERMINATE; // CALL(PCMarraysort (list, list->length, 0)); //if ( error ) goto TERMINATE; CALL(PCMarrayoutput (list)); CALL(PCMarrayfree (list)); list = NULL; printf ("You have input %d integers\n", count); printf ("The max one is %d\n" , max); printf ("The sum of them is %d\n" , sum); TERMINATE: PCMcheckerror(error); if ( NULL != list ) PCMarrayfree(list); }
int main (void) { int i = 0; int error = 0; error = inout(); if ( error ) goto TERMINATE; TERMINATE: PCMcheckerror (error); return 0; }
void test_PCM_array_insert(void) { int i; int error = 0; printf ("init my list\n"); PCMarray* mylist = PCMarraynew("list4"); printf ("insert 1-3 elements"); for (i = 0; i < 3; ++i) { CALL(PCMarrayinsert (mylist, i, i)); } CALL(PCMarrayoutput(mylist)); printf ("insert 3-5 elements"); for (i = 3; i < PCMLISTINITSIZE ; ++i) { CALL(PCMarrayinsert (mylist, i, i)); } CALL(PCMarrayoutput(mylist)); printf ("insert 5-10 elements"); for (i = 5; i < 10; ++i) { CALL(PCMarrayinsert (mylist, i, i)); } CALL(PCMarrayoutput(mylist)); printf ("free my list\n"); CALL(PCMarrayfree (mylist)); mylist = NULL; if ( mylist == NULL ) printf("free mylist successfully!"); TERMINATE: PCMcheckerror (error); if ( NULL != mylist ) PCMarrayfree (mylist); }
int main(void) { int i; int error = 0; FILE *fp; char line[256]; PCMlinkedlist *mylist1 = NULL; PCMlinkedlist *mylist2 = NULL; fp = fopen ("../data/git.txt", "r"); if ( fp == NULL) { printf ("PCM Error: fopen error!\n"); goto TERMINATE; } error = PCMlinkedlistinit (&mylist1); if ( error ) goto TERMINATE; error = PCMlinkedlistinit (&mylist2); if ( error ) goto TERMINATE; while (fgets (line, 256, fp) != NULL) { error = PCMlinkedlistappend (mylist1, line); if ( error ) goto TERMINATE; if ( strstr (line, "malloc") != NULL) { error = PCMlinkedlistappend (mylist2, line); if ( error ) goto TERMINATE; } } PCMlinkedlistoutput (mylist1, NULL); PCMlinkedlistoutput (mylist2, NULL); PCMlinkedlistfree (&mylist1); PCMlinkedlistfree (&mylist2); fclose (fp); TERMINATE: PCMcheckerror (error); return 0; }
void test_PCM_array_init_and_free(void) { int i; int error = 0; /* int arr[10]; */ /* PCMarray mylist; */ /* mylist.elemp = arr; */ /* mylist.length = 1; */ /* mylist.capacity = 10; */ /* CALL(PCMarrayinsert (&mylist, 0, 19); */ /* if ( error ) goto TERMINATE; */ PCMarray* mylist = PCMarraynew("list1"); CALL(PCMarrayoutput(mylist)); CALL(PCMarrayfree (mylist)); mylist = NULL; if ( mylist == NULL ) printf("free mylist successfully!"); TERMINATE: PCMcheckerror (error); if ( NULL != mylist ) PCMarrayfree (mylist); }
int main (void) { int i = 0; size_t len = 0; int error = 0; char str[5] = {0}; /* char str[] = {'T', 'h', 'i', 's', '\0'}; */ /* char str[] = "Thip"; */ /* char *str = "Thip"; */ /* str[1] = 'o'; */ // test PCMstrlen and PCMstrtrim char str1[] = ""; char str2[] = " "; char str3[] = "a b "; char str4[] = "ab "; CALL(PCMstrlen (str1, &len)); printf ("str1 = %s, len = %d\n", str1,len); CALL(PCMstrlen (str2, &len)); printf ("str2 = %s, len = %d\n", str2,len); CALL(PCMstrtrim (str1)); CALL(PCMstrtrim (str2)); CALL(PCMstrtrim (str3)); CALL(PCMstrtrim (str4)); CALL(PCMstrlen (str1, &len)); printf ("str1 = %s, %d\n", str1, len); CALL(PCMstrlen (str2, &len)); printf ("str2 = %s, %d\n", str2, len); CALL(PCMstrlen (str3, &len)); printf ("str3 = %s, %d\n", str3, len); CALL(PCMstrlen (str4, &len)); printf ("str4 = %s, %d\n", str4, len); // test PCMstreverse PCMstrncpy(str, "Thisisastring", sizeof(str)); /* strncpy(str, "Thisisastring", sizeof(str)); */ printf ("str = %s\n", str); CALL(PCMstreverse (str)); printf ("str = %s\n", str); CALL(PCMstreverse (str)); printf ("str = %s\n", str); // test PCMstrdelcharofstring and PCMstrlen /* strncat(str, "This is a string", 10); */ /* strncpy(str, "This is a string", 10); */ CALL(PCMstrlen (str, &len)); printf ("str = %s, len = %d\n", str,len); CALL(PCMdelcharofstring(str, 'p')); printf ("%s",str); printf ("\n"); CALL(PCMdelcharofstring(str, 't')); printf ("%s", str); printf ("\n"); CALL(PCMstrlen (str, &len)); printf ("str = %s, len = %d\n", str,len); // test PCMstrncat function char tail[] = "test_PCMstrncat"; /* CALL(PCMstrncat (str, tail, sizeof tail); */ CALL(PCMstrncat (str, tail, -1)); CALL(PCMstrlen (str, &len)); printf ("str = %s, len = %d\n", str,len); // test PCMstrblkstoblk char bstr[] = " test PCMstrblkstoblk !"; /* char bstr[] = "ab "; */ /* char bstr[] = " ab "; */ /* char bstr[] = " a b "; */ CALL(PCMstrblkstoblk (bstr)); CALL(PCMstrlen (bstr, &len)); printf ("bstr = %s, len = %d\n", bstr,len); // test PCMstrindex char bigstr[] = "defefgdefk"; char substr[] = "def"; int index; CALL(PCMstrindex (bigstr, substr, &index)); printf ("The last %s in %s at %d\n", substr, bigstr, index); //CALL(PCMdelcharofstring(NULL, 't')); //printf ("%s",str); //printf ("\n"); TERMINATE: PCMcheckerror (error); return 0; }
int main(void) { int i = 0; int error = 0; int a[5] = {1, 3, 4, 5, 2}; float b[5] = {1.0, 3.0, 4.0, 5.0, 2.0}; char c[5] = {'a', 'b', 'c', 'd', '\0'}; char *strarr[5] = {"ab", "cd", "ef", "g", "h"}; char *dynamicstrarr[5] = {0}; void *p = NULL; PCMstack *s = NULL; CALL(PCMstackcreate (&s, 3)); //integer printf ("integer stack:\n"); for (i = 0; i < 5; ++i) { CALL(PCMstackpush (s, &a[i])); } for (i = 0; i < 5; ++i) { printf ("%5d", *(int*)s->data[i]); } putchar('\n'); for (i = 0; i < 5; ++i) { printf ("%d is poped\n", *(int*)(PCMstackpop(s))); } if ( PCMstackisempty(s) ) { printf ("stack is empty now!\n"); } //float printf ("float stack:\n"); for (i = 0; i < 5; ++i) { CALL(PCMstackpush (s, &b[i])); } for (i = 0; i < 5; ++i) { printf ("%5.2f", *(float*)s->data[i]); } putchar('\n'); for (i = 0; i < 5; ++i) { printf ("%.2f is poped\n", *(float*)(PCMstackpop(s))); } if ( PCMstackisempty(s) ) { printf ("stack is empty now!\n"); } //char printf ("char stack:\n"); for (i = 0; i < 5; ++i) { CALL(PCMstackpush (s, &c[i])); } for (i = 0; i < 5; ++i) { printf ("%5c", *(char*)s->data[i]); } putchar('\n'); for (i = 0; i < 5; ++i) { printf ("%c is poped\n", *(char*)(PCMstackpop(s))); } if ( PCMstackisempty(s) ) { printf ("stack is empty now!\n"); } //strarray printf ("str array stack:\n"); for (i = 0; i < 5; ++i) { CALL(PCMstackpush (s, strarr[i])); } for (i = 0; i < 5; ++i) { printf ("%5s", (char*)s->data[i]); } putchar('\n'); for (i = 0; i < 5; ++i) { printf ("%s is poped\n", (char*)(PCMstackpop(s))); } if ( PCMstackisempty(s) ) { printf ("stack is empty now!\n"); } //dynamic string array printf ("dynamic string array:\n"); for (i = 0; i < 5; ++i) { dynamicstrarr[i] = NULL; dynamicstrarr[i] = malloc (sizeof(char*)); strcpy(dynamicstrarr[i], strarr[i]); CALL(PCMstackpush (s, dynamicstrarr[i])); } for (i = 0; i < 5; ++i) { printf ("%5s", (char*)s->data[i]); } putchar('\n'); /* for (i = 0; i < 5; ++i) { */ /* p = PCMstackpop(s); */ /* printf ("%s is poped\n", (char*)p); */ /* free (p); */ /* } */ if ( PCMstackisempty(s) ) { printf ("stack is empty now!\n"); } PCMstackfree (&s, free); TERMINATE: PCMstackfree (&s, free); PCMcheckerror (error); return 0; }
void test_PCM_array_sort_and_find(void) { int i; int error = 0; int ind; printf("sort_and_find\n"); PCMarray* mylist = PCMarraynew("list7"); printf("after init\n"); CALL(PCMarrayinsert (mylist, 0, 1)); CALL(PCMarrayinsert (mylist, 1, 10)); CALL(PCMarrayinsert (mylist, 2, 0)); CALL(PCMarrayinsert (mylist, 3, 5)); for (i = 0; i < 3; ++i) { CALL(PCMarrayinsert (mylist,i,i*2)); } for (i = 0; i < 3; ++i) { CALL(PCMarrayinsert (mylist,i,i)); } PCMarrayoutput (mylist); printf ("sort mylist\n"); enum PCMSORTALG sortalg = PCMALGSORTBUBBLE; CALL(PCMarraysort (mylist, mylist->length, sortalg)); printf("output mylist after bubble sort\n"); PCMarrayoutput (mylist); //enum PCMSORTALG sortalg = PCMALGSORTSELECT; //enum PCMSORTALG sortalg = PCMALGSORTQUICK; // use qsort*/ //enum PCMSORTALG sortalg = PCMALGSORTSHELL; //enum PCMSORTALG sortalg = PCMALGSORTINSERT; printf ("find the index of element '5'\n"); enum PCMSEARCHALG findalg = PCMALGORIGINFIND; for (i = 0; i < 1000; ++i) { CALL(PCMarrayfind (mylist, mylist->length, 5, &ind, findalg)); } printf ("Index of 5 is %d\n", ind); printf ("find the index of element '5'\n"); findalg = PCMALGBINFIND; for (i = 0; i < 1000; ++i) { CALL(PCMarrayfind (mylist, mylist->length, 5, &ind, findalg)); } printf ("Index of 5 is %d\n", ind); /* printf ("Unit test two:\n"); error = PCMarrayinsert (mylist, 1, 8); if ( error ) goto TERMINATE; PCMarraysort (mylist, mylist->length); PCMarrayoutput (mylist); error = PCMarrayinsert (mylist, 0, 100); if ( error ) goto TERMINATE; error = PCMarrayinsert (mylist, 2, 1000); if ( error ) goto TERMINATE; PCMarraysort(mylist, mylist->length); PCMarrayoutput (mylist); error = PCMarrayDelete (mylist, 3, &e); if ( error ) goto TERMINATE; PCMarrayoutput (mylist); PCMarraysort (mylist, mylist->length); error = PCMarrayClear (mylist); PCMarrayoutput (mylist); error = PCMarrayinsert (mylist, 0, 100); if ( error ) goto TERMINATE; error = PCMarrayinsert (mylist, 1, 1000); PCMarrayoutput (mylist); if ( error ) goto TERMINATE;*/ CALL(PCMarrayfree (mylist)); mylist = NULL; if ( mylist == NULL ) printf("sqlist free successfully!\n"); TERMINATE: PCMcheckerror (error); }