1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#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)
|