2006-08-18 Aaron Bockover <abockover@novell.com>
[mono.git] / eglib / test / string.c
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
5
6 #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);}
7
8 RESULT
9 test_gstring ()
10 {
11         GString *s = g_string_new_len ("My stuff", 2);
12         char *ret;
13         int i;
14
15         if (strcmp (s->str, "My") != 0)
16                 return "Expected only 'My' on the string";
17         g_string_free (s, TRUE);
18
19         s = g_string_new_len ("My\0\0Rest", 6);
20         if (s->str [2] != 0)
21                 return "Null was not copied";
22         if (strcmp (s->str+4, "Re") != 0){
23                 return "Did not find the 'Re' part";
24         }
25
26         g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
27         if (s->str [2] != 0)
28                 return "Null as not copied";
29         if (strncmp (s->str+4, "Relala", 6) != 0){
30                 return FAILED("Did not copy correctly, got: %s", s->str+4);
31         }
32
33         g_string_free (s, TRUE);
34         s = g_string_new ("");
35         for (i = 0; i < 1024; i++){
36                 g_string_append (s, "x");
37         }
38         if (strlen (s->str) != 1024){
39                 return FAILED("Incorrect string size, got: %s %d", s->str, strlen (s->str));
40         }
41         g_string_free (s, TRUE);
42
43         s = g_string_new ("");
44         for (i = 0; i < 1024; i++){
45                 g_string_append_c (s, 'x');
46         }
47         if (strlen (s->str) != 1024){
48                 return FAILED("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
49         }
50         g_string_free (s, TRUE);
51
52         s = g_string_new ("hola");
53         g_string_sprintfa (s, "%s%d", ", bola", 5);
54         if (strcmp (s->str, "hola, bola5") != 0){
55                 return FAILED("Incorrect data, got: %s\n", s->str);
56         }
57         g_string_free (s, TRUE);
58
59         s = g_string_new ("Hola");
60         g_string_printf (s, "Dingus");
61         
62         /* Test that it does not release it */
63         ret = g_string_free (s, FALSE);
64         g_free (ret);
65
66         s = g_string_new_len ("H\000H", 3);
67         g_string_append_len (s, "1\0002", 3);
68         sfail ('H', 0);
69         sfail ( 0, 1);
70         sfail ('H', 2);
71         sfail ('1', 3);
72         sfail ( 0, 4);
73         sfail ('2', 5);
74         g_string_free (s, TRUE);
75         
76         return OK;
77 }
78
79 static Test string_tests [] = {
80         {"GString", test_gstring},
81         {NULL, NULL}
82 };
83
84 DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)