[dhcp-agent-commits] dhcp-agent/tests .cvsignore,NONE,1.1 Makefile.am,NONE,1.1 tests-stringbuffer.c,
Status: Alpha
Brought to you by:
actmodern
From: <act...@us...> - 2002-12-30 00:47:54
|
Update of /cvsroot/dhcp-agent/dhcp-agent/tests In directory sc8-pr-cvs1:/tmp/cvs-serv13118/tests Added Files: .cvsignore Makefile.am tests-stringbuffer.c tests.h Log Message: added tests --- NEW FILE: .cvsignore --- Makefile.in Makefile stamp-h1 .deps .libs test-stringbuffer --- NEW FILE: Makefile.am --- # $Header: /cvsroot/dhcp-agent/dhcp-agent/tests/Makefile.am,v 1.1 2002/12/30 00:47:52 actmodern Exp $ # # Tests Makefile.am AUTOMAKE_OPTIONS = foreign AM_CFLAGS = -Wall -Werror check_PROGRAMS = stringbuffertest INCLUDES = -I../src stringbuffertest_SOURCES = tests-stringbuffer.c tests.h LDADD = ../src/libdhcputil.la TESTS = stringbuffertest --- NEW FILE: tests-stringbuffer.c --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/tests/tests-stringbuffer.c,v 1.1 2002/12/30 00:47:52 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tm...@wh...> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * stringbuffer test code. we need more tests too :) * */ #include "dhcp-local.h" #include "dhcp-libutil.h" #include "tests.h" #define TEST_NAME "stringbuffer" /* test strings. */ /* two basic strings for appending. */ #define BAR_STRING "This is my source code, there are many others like it, but this one is mine." #define FOO_STRING "abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890" /* the same strings appended 5 times and appended 10 times */ #define FOO_STRING_X10 "abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890" #define FOO_STRING_X5 "abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890" #define BAR_STRING_X5 "This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine." #define BAR_STRING_X10 "This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine.This is my source code, there are many others like it, but this one is mine." /* the same strings appended 5 and 10 times only with a " : " in between to test aprintf capability */ #define BAR_STRING_X5_PRINTF "This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : " #define BAR_STRING_X10_PRINTF "This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : This is my source code, there are many others like it, but this one is mine. : " #define FOO_STRING_X5_PRINTF "abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : " #define FOO_STRING_X10_PRINTF "abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : abcdefghijklmnopqrstuvwxABCDEFGHIKLMNOPQRSTUVWXZ!@#$%^&*()_+1234567890 : " /* two strings to replace. */ #define REPLACE_FOO_1 "foo bar foo bar foo bar foo bar foo bar" #define REPLACE_FOO_2 "foo foo foo foo foo foo foo foo foo foo" #define REPLACE_BAR_1 "bar foo bar foo bar foo bar foo bar foo" #define REPLACE_BAR_2 "bar bar bar bar bar bar bar bar bar bar" #define REPLACE_FOO "foo" #define REPLACE_BAR "bar" /* utility routines. */ static int test_util_string_is_set(stringbuffer_t *sb, const char *string, const char *name) { const char *ptr; int len; /* see if it returns properly. */ ptr = stringbuffer_getstring(sb); if(strcmp(ptr, string)) { TEST_ERROR(name, "string did not match properly : string comparison wrong"); return 1; } len = stringbuffer_getlen(sb); if(len != strlen(ptr)) { TEST_ERROR(name, "string did not match properly : string length wrong"); return 1; } return 0; } /* test routines. */ static int test_create_destroy(const char *name) { stringbuffer_t *sb; /* just create and destroy. we cannot catch an error here * but we may crash if something is up. */ sb = stringbuffer_create(); stringbuffer_destroy(sb); return 0; } static int test_append_clear(const char *name) { stringbuffer_t *sb; int i; sb = stringbuffer_create(); /* * * * * * * * * * * * * * * * * Append BAR_STRING and test. * * * * * * * * * * * * * * * * */ /* append bar string. */ stringbuffer_append(sb, BAR_STRING); /* check. */ if(test_util_string_is_set(sb, BAR_STRING, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; stringbuffer_clear(sb); for(i = 0;i < 5;i ++) /* do 5 X. */ stringbuffer_append(sb, BAR_STRING); if(test_util_string_is_set(sb, BAR_STRING_X5, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; for(i = 0;i < 10;i ++) /* do 10 X. */ stringbuffer_append(sb, BAR_STRING); if(test_util_string_is_set(sb, BAR_STRING_X10, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; /* * * * * * * * * * * * * * * * * Append FOO_STRING and test. * * * * * * * * * * * * * * * * */ /* append bar string. */ stringbuffer_append(sb, FOO_STRING); /* check. */ if(test_util_string_is_set(sb, FOO_STRING, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; stringbuffer_clear(sb); for(i = 0;i < 5;i ++) /* do 5 X. */ stringbuffer_append(sb, FOO_STRING); if(test_util_string_is_set(sb, FOO_STRING_X5, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; for(i = 0;i < 10;i ++) /* do 10 X. */ stringbuffer_append(sb, FOO_STRING); if(test_util_string_is_set(sb, FOO_STRING_X10, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; /* done. */ stringbuffer_destroy(sb); return 0; } static int test_aprintf_clear(const char *name) { stringbuffer_t *sb; int i; sb = stringbuffer_create(); /* * * * * * * * * * * * * * * * * Append BAR_STRING and test. * * * * * * * * * * * * * * * * */ /* append bar string. */ stringbuffer_aprintf(sb, "%s", BAR_STRING); /* check. */ if(test_util_string_is_set(sb, BAR_STRING, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; stringbuffer_clear(sb); for(i = 0;i < 5;i ++) /* do 5 X. */ stringbuffer_aprintf(sb, "%s : ", BAR_STRING); if(test_util_string_is_set(sb, BAR_STRING_X5_PRINTF, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; for(i = 0;i < 10;i ++) /* do 10 X. */ stringbuffer_aprintf(sb, "%s : ", BAR_STRING); if(test_util_string_is_set(sb, BAR_STRING_X10_PRINTF, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; /* * * * * * * * * * * * * * * * * Append FOO_STRING and test. * * * * * * * * * * * * * * * * */ /* append bar string. */ stringbuffer_append(sb, FOO_STRING); /* check. */ if(test_util_string_is_set(sb, FOO_STRING, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; stringbuffer_clear(sb); for(i = 0;i < 5;i ++) /* do 5 X. */ stringbuffer_aprintf(sb, "%s : ", FOO_STRING); if(test_util_string_is_set(sb, FOO_STRING_X5_PRINTF, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; for(i = 0;i < 10;i ++) /* do 10 X. */ stringbuffer_aprintf(sb, "%s : ", FOO_STRING); if(test_util_string_is_set(sb, FOO_STRING_X10_PRINTF, name)) return 1; stringbuffer_clear(sb); /* check to see if it matches the empty string. */ if(test_util_string_is_set(sb, "", name)) return 1; /* done. */ stringbuffer_destroy(sb); return 0; } static int test_replace_c(const char *name) { stringbuffer_t *sb; sb = stringbuffer_create(); /* * * * * * * * * * * * * * * * * * using replace_c only change * * foo to brr * * * * * * * * * * * * * * * * * */ stringbuffer_append(sb, REPLACE_FOO); stringbuffer_replace_c(sb, 'f', 'b'); stringbuffer_replace_c(sb, 'o', 'r'); if(test_util_string_is_set(sb, "brr", name)) return 1; stringbuffer_destroy(sb); return 0; } static int test_replace(const char *name) { stringbuffer_t *sb; sb = stringbuffer_create(); /* * * * * * * * * * * * * * * * * * replace two simple sequences. * * * * * * * * * * * * * * * * * */ stringbuffer_append(sb, REPLACE_FOO_1); stringbuffer_replace(sb, "bar", "foo"); if(test_util_string_is_set(sb, REPLACE_FOO_2, name)) return 1; stringbuffer_clear(sb); stringbuffer_append(sb, REPLACE_BAR_1); stringbuffer_replace(sb, "foo", "bar"); if(test_util_string_is_set(sb, REPLACE_BAR_2, name)) return 1; stringbuffer_clear(sb); /* * * * * * * * * * * * * * * * * * * * * replace one string with one string * * * * * * * * * * * * * * * * * * * * */ stringbuffer_append(sb, REPLACE_FOO); stringbuffer_replace(sb, REPLACE_FOO, REPLACE_BAR); if(test_util_string_is_set(sb, REPLACE_BAR, name)) return 1; stringbuffer_clear(sb); /* * * * * * * * * * * * * * * * * * * * replace one string with itself. * * * * * * * * * * * * * * * * * * * */ stringbuffer_append(sb, REPLACE_FOO); stringbuffer_replace(sb, REPLACE_FOO, REPLACE_FOO); if(test_util_string_is_set(sb, REPLACE_FOO, name)) return 1; stringbuffer_clear(sb); /* * * * * * * * * * * * * * * * * * * * * * replace two charactes in "foo" to be * * "brr" with replace * * * * * * * * * * * * * * * * * * * * * */ stringbuffer_append(sb, REPLACE_FOO); stringbuffer_replace(sb, "f", "b"); stringbuffer_replace(sb, "o", "r"); if(test_util_string_is_set(sb, "brr", name)) return 1; stringbuffer_clear(sb); /* done. */ stringbuffer_destroy(sb); return 0; } /* array of test routines. */ static test_table tests[] = { { "create/destroy", test_create_destroy }, { "append/clear", test_append_clear }, { "aprintf/clear", test_aprintf_clear }, { "replace_c", test_replace_c }, { "replace", test_replace }, }; /* main: loops. */ int main(int argc, char *argv[]) { DO_TESTS(tests); exit(TESTS_EXIT_SUCCESS); } --- NEW FILE: tests.h --- /* $Header: /cvsroot/dhcp-agent/dhcp-agent/tests/tests.h,v 1.1 2002/12/30 00:47:52 actmodern Exp $ * * Copyright 2002 Thamer Alharbash <tm...@wh...> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. The names of the authors may not be used to endorse or promote * products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * */ #ifndef TESTS_H #define TESTS_H /* types */ /* test function. */ typedef int (*test_t)(const char *name); typedef struct { const char *name; test_t test_func; } test_table; /* exit values. */ #define TESTS_EXIT_FAILURE 1 #define TESTS_EXIT_SUCCESS 0 #define TESTS_EXIT_IGNORE 77 /* macros. */ #define TEST_ERROR(name, x) { fprintf(stderr, "test error (%s):\n", name); fprintf(stderr, "%s\n", x); } #define TEST_MSG(x) { fprintf(stdout, "%s\n", x); } #define TEST_NELMS(x) (sizeof(x)/sizeof(x[0])) #define DO_TESTS(x) { \ int i; \ for(i = 0;i < TEST_NELMS(x);i++) { \ if(x[i].test_func(x[i].name)) \ exit(TESTS_EXIT_FAILURE); \ } \ } #endif /* TESTS_H */ |