Files
mono/eglib/test/string.c
T
Jonathan Chambers 10f6ce36d0 2007-04-27 Jonathan Chambers <[email protected]>
Combined patch from Michael Jerris <[email protected]> and me.
	* winconfig.h: Config file for msvc builds.
	* configure.ac: Add AC_CHECK_HEADERS for getopt.h,  sys/time.h,
	sys/wait.h, and pwd.h.
	
	* src/gspawn.c: Include config.h and use HAVE_* macros.
	* src/eglib-config.h.in: Add inclusion guards.
	* src/ghashtable.c: Fix warnings.
	* src/gmisc.c: Windows implementation of g_getenv, g_setenv,
	and g_unsetenv.
	* src/gpath.c: Include config.h and use HAVE_* macros.
	* src/gmarkup.c: Fix warnings.
	* src/gptrarray.c: Fix warnings.
	* src/gstr.c: Fix warnings and use HAVE_* macros.
	* src/glib.h: Move macros to eglib-config.h and include it.
	* src/gdate.c: Implement g_get_current_time on windows.
	* src/gpattern.c: Fix warnings.
	* src/gdir.c: Implement g_dir on windows.
	* src/gfile.c: Implement mkstemp and g_file_test on windows.
	* src/gtimer.c: Implement g_timer on windows.
	* src/gmodule.c: Fix warnings.
	* src/gunicode.c: Fix warnings.
	* src/gutf8.c: Fix warnings.
	
	* test/ptrarray.c: Fix warnings.
	* test/string.c: Fix warnings.
	* test/list.c: Fix warnings.
	* test/pattern.c: Use HAVE_* macros.
	* test/dir.c: Use HAVE_* macros.
	* test/file.c: Use HAVE_* macros and use system specific files for tests.
	* test/test.c: Fix warnings and use HAVE_* macros.
	* test/timer.c: Use HAVE_* macros.
	* test/string-util.c: Fix warnings.
	* test/test.h: Supress warnings on msvc.
	* test/module.c: Use HAVE_* macros.
	* test/utf8.c: Fix warnings.
	* test/spawn.c: Use HAVE_* macros.
	* test/driver.c: Fix warnings and use HAVE_* macros.
	* test/path.c: Use HAVE_* macros and use system specific files for tests.
	* test/sizes.c: Fix warnings.

2007-04-27  Jonathan Chambers <[email protected]>
	
	Combined patch from Michael Jerris <[email protected]> and me.
	* getopt.c: Added public domain implementation for Windows.
	* getopt.h: Added public domain implementation for Windows.
	* getopt_long.c: Added public domain implementation for Windows.
	* test_eglib.vcproj: Update project.
	* eglib.vcproj: Update project. Add pre-build step to copy
	winconfig.h to config.h for msvc build.


svn path=/trunk/mono/; revision=76406
2007-04-27 20:12:25 +00:00

238 lines
5.1 KiB
C

#include <glib.h>
#include <string.h>
#include <stdio.h>
#include "test.h"
#define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return FAILED("Got %s, Failed at %d, expected '%c'", s->str, p, k);}
RESULT
test_append_speed()
{
GString *s = g_string_new("");
gint i;
for(i = 0; i < 1024; i++) {
g_string_append(s, "x");
}
if(strlen (s->str) != 1024) {
return FAILED("Incorrect string size, got: %s %d",
s->str, strlen(s->str));
}
g_string_free (s, TRUE);
return OK;
}
RESULT
test_append_c_speed()
{
GString *s = g_string_new("");
gint i;
for(i = 0; i < 1024; i++) {
g_string_append_c(s, 'x');
}
if(strlen(s->str) != 1024) {
return FAILED("Incorrect string size, got: %s %d", s->str,
strlen(s->str));
}
g_string_free(s, TRUE);
return OK;
}
RESULT
test_gstring ()
{
GString *s = g_string_new_len ("My stuff", 2);
char *ret;
int i;
if (strcmp (s->str, "My") != 0)
return "Expected only 'My' on the string";
g_string_free (s, TRUE);
s = g_string_new_len ("My\0\0Rest", 6);
if (s->str [2] != 0)
return "Null was not copied";
if (strcmp (s->str+4, "Re") != 0){
return "Did not find the 'Re' part";
}
g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
if (s->str [2] != 0)
return "Null as not copied";
if (strncmp (s->str+4, "Relala", 6) != 0){
return FAILED("Did not copy correctly, got: %s", s->str+4);
}
g_string_free (s, TRUE);
s = g_string_new ("");
for (i = 0; i < 1024; i++){
g_string_append_c (s, 'x');
}
if (strlen (s->str) != 1024){
return FAILED("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
}
g_string_free (s, TRUE);
s = g_string_new ("hola");
g_string_sprintfa (s, "%s%d", ", bola", 5);
if (strcmp (s->str, "hola, bola5") != 0){
return FAILED("Incorrect data, got: %s\n", s->str);
}
g_string_free (s, TRUE);
s = g_string_new ("Hola");
g_string_printf (s, "Dingus");
/* Test that it does not release it */
ret = g_string_free (s, FALSE);
g_free (ret);
s = g_string_new_len ("H" "\000" "H", 3);
g_string_append_len (s, "1" "\000" "2", 3);
sfail ('H', 0);
sfail ( 0, 1);
sfail ('H', 2);
sfail ('1', 3);
sfail ( 0, 4);
sfail ('2', 5);
g_string_free (s, TRUE);
return OK;
}
RESULT
test_sized ()
{
GString *s = g_string_sized_new (20);
if (s->str [0] != 0)
return FAILED ("Expected an empty string");
if (s->len != 0)
return FAILED ("Expected an empty len");
g_string_free (s, TRUE);
return NULL;
}
RESULT
test_truncate ()
{
GString *s = g_string_new ("0123456789");
g_string_truncate (s, 3);
if (strlen (s->str) != 3)
return FAILED ("size of string should have been 3, instead it is [%s]\n", s->str);
g_string_free (s, TRUE);
s = g_string_new ("a");
s = g_string_truncate (s, 10);
if (strlen (s->str) != 1)
return FAILED ("The size is not 1");
g_string_truncate (s, (gsize)-1);
if (strlen (s->str) != 1)
return FAILED ("The size is not 1");
g_string_truncate (s, 0);
if (strlen (s->str) != 0)
return FAILED ("The size is not 0");
g_string_free (s, TRUE);
return NULL;
}
RESULT
test_prepend ()
{
GString *s = g_string_new ("dingus");
g_string_prepend (s, "one");
if (strcmp (s->str, "onedingus") != 0)
return FAILED ("Failed, expected onedingus, got [%s]", s->str);
g_string_free (s, TRUE);
/* This is to force the code that where stuff does not fit in the allocated block */
s = g_string_sized_new (1);
g_string_prepend (s, "one");
if (strcmp (s->str, "one") != 0)
return FAILED ("Got erroneous result, expected [one] got [%s]", s->str);
g_string_free (s, TRUE);
/* This is to force the path where things fit */
s = g_string_new ("123123123123123123123123");
g_string_truncate (s, 1);
if (strcmp (s->str, "1") != 0)
return FAILED ("Expected [1] string, got [%s]", s->str);
g_string_prepend (s, "pre");
if (strcmp (s->str, "pre1") != 0)
return FAILED ("Expected [pre1], got [%s]", s->str);
g_string_free (s, TRUE);
return NULL;
}
RESULT
test_appendlen ()
{
GString *s = g_string_new ("");
g_string_append_len (s, "boo\000x", 0);
if (s->len != 0)
return FAILED ("The length is not zero %d", s->len);
g_string_append_len (s, "boo\000x", 5);
if (s->len != 5)
return FAILED ("The length is not five %d", s->len);
g_string_append_len (s, "ha", -1);
if (s->len != 7)
return FAILED ("The length is not seven %d", s->len);
g_string_free (s, TRUE);
return NULL;
}
RESULT
test_macros ()
{
char *s = g_strdup (G_STRLOC);
char *p = strchr (s + 2, ':');
int n;
if (p == NULL)
return FAILED ("Did not find a separator");
n = atoi (p+1);
if (n <= 0)
return FAILED ("did not find a valid line number");
*p = 0;
if (strcmp (s + strlen(s) - 8 , "string.c") != 0)
return FAILED ("This did not store the filename on G_STRLOC");
g_free (s);
return NULL;
}
static Test string_tests [] = {
{"append-speed", test_append_speed},
{"append_c-speed", test_append_c_speed},
{"ctor+append", test_gstring },
{"ctor+sized", test_sized },
{"truncate", test_truncate },
{"prepend", test_prepend },
{"append_len", test_appendlen },
{"macros", test_macros },
{NULL, NULL}
};
DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)