comparison tests/test-obstack-printf.c @ 10207:e027c5e42aba

Improve obstack-printf test code. * tests/test-obstack-printf.c (test_function): Fix comment, and simplify usage of obstack_* in macros. Add a test for coverage. Reported by Bruno Haible. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Sat, 14 Jun 2008 07:36:03 -0600
parents 3384541effec
children cee1ca4b9566
comparison
equal deleted inserted replaced
10206:927afe014a7d 10207:e027c5e42aba
45 static void 45 static void
46 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...)) 46 test_function (int (*my_obstack_printf) (struct obstack *, const char *, ...))
47 { 47 {
48 struct obstack obs; 48 struct obstack obs;
49 obstack_init (&obs); 49 obstack_init (&obs);
50 /* In general, don't invoke obstack_* functions inside ASSERT, as 50 /* In general, be careful that arguments to obstack_* don't have
51 not all compilers can avoid multiple side effects. */ 51 side effects, as not all compilers evaluate macro arguments only
52 once. */
52 53
53 /* Grow the obstack to near its boundary, then check that output 54 /* Grow the obstack to near its boundary, then check that short
54 longer than the obstack free space grows the obstack. */ 55 output longer than the obstack free space grows the obstack. */
55 { 56 {
56 char *base = obstack_base (&obs); 57 char *base = obstack_base (&obs);
57 char *new_base; 58 char *new_base;
58 int result; 59 int result;
59 int size;
60 int room = obstack_room (&obs) - 4; 60 int room = obstack_room (&obs) - 4;
61 61
62 obstack_blank_fast (&obs, room); 62 obstack_blank_fast (&obs, room);
63 result = my_obstack_printf (&obs, "%d %s", 123, "456"); 63 result = my_obstack_printf (&obs, "%d %s", 123, "456");
64 ASSERT (result == 7); 64 ASSERT (result == 7);
65 size = obstack_object_size (&obs); 65 ASSERT (result + room == obstack_object_size (&obs));
66 ASSERT (result + room == size);
67 obstack_1grow (&obs, 0); 66 obstack_1grow (&obs, 0);
68 new_base = obstack_finish (&obs); 67 new_base = obstack_finish (&obs);
69 ASSERT (base != new_base); 68 ASSERT (base != new_base);
70 ASSERT (strcmp (new_base + room, "123 456") == 0); 69 ASSERT (strcmp (new_base + room, "123 456") == 0);
71 } 70 }
74 cause a reshuffling of the obstack. */ 73 cause a reshuffling of the obstack. */
75 { 74 {
76 char *base = obstack_base (&obs); 75 char *base = obstack_base (&obs);
77 char *new_base; 76 char *new_base;
78 int result; 77 int result;
79 int size;
80 int room = obstack_room (&obs); 78 int room = obstack_room (&obs);
81 79
82 ASSERT (8 < room); 80 ASSERT (8 < room);
83 result = my_obstack_printf (&obs, "%d %s", 123, "456"); 81 result = my_obstack_printf (&obs, "%d %s", 123, "456");
84 ASSERT (result == 7); 82 ASSERT (result == 7);
85 size = obstack_object_size (&obs); 83 ASSERT (result == obstack_object_size (&obs));
86 ASSERT (result == size);
87 new_base = obstack_base (&obs); 84 new_base = obstack_base (&obs);
88 ASSERT (base == new_base); 85 ASSERT (base == new_base);
89 ASSERT (strncmp (base, "123 456", result) == 0); 86 ASSERT (strncmp (base, "123 456", result) == 0);
87 obstack_finish (&obs);
88 }
89
90 /* Check for generating much more output than a chunk size. */
91 {
92 char *base = obstack_base (&obs);
93 char *new_base;
94 int result;
95 int i;
96
97 ASSERT (obstack_chunk_size (&obs) < 10000);
98 result = my_obstack_printf (&obs, "%010000d", 0);
99 ASSERT (result == 10000);
100 ASSERT (result == obstack_object_size (&obs));
101 new_base = obstack_base (&obs);
102 ASSERT (base != new_base);
103 for (i = 0; i < 10000; i++)
104 ASSERT (new_base[i] == '0');
90 } 105 }
91 106
92 obstack_free (&obs, NULL); 107 obstack_free (&obs, NULL);
93 } 108 }
94 109