ppnumsplit fix
[mala.git] / tests / test-mala_strings.c
blob4905ac7b9db265e5b8f4cd005ab639325f4040e7
1 /*
2 * Copyright (C) 2004, Christian Thaeter <chth@gmx.net>
4 * This file is part of the MaLa extension Language.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, contact me.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <nobug.h>
22 #include <acogc.h>
24 #include "../engine/strings.h"
25 #include "../engine/stringbucket.h"
27 #define TEST(t) printf("TEST %s: ",#t);\
28 if((t))printf(".. OK\n");\
29 else printf(".. FAILED\n")
31 #define TEST_VOID(t) printf("TEST %s: ",#t);\
32 fflush(stdout); t; printf(".. OK\n")
34 acogc_root gcroot;
35 acogc_factory bucketfactory;
36 acogc_factory gcfactory_strings;
37 acogc_factory gcfactory_splaynodes;
39 void
40 construction (MalaStringBucket testbucket)
42 ACOGC_STACK_ENTER (testbucket->gcroot);
43 ACOGC_STACK_PTR (MalaString, a);
44 ACOGC_STACK_PTR (MalaString, b);
45 ACOGC_STACK_PTR (MalaString, c);
46 ACOGC_STACK_PTR (MalaString, d);
47 ACOGC_STACK_PTR (MalaString, e);
48 ACOGC_STACK_PTR (MalaString, onetwothreefour);
50 TEST(a.ptr = mala_string_new_cstr ("bar", testbucket));
52 TEST(b.ptr = mala_string_new_cstr ("baz", testbucket));
54 TEST(c.ptr = mala_string_new_cstr ("bar", testbucket));
56 TEST(a.ptr == c.ptr);
58 TEST(mala_string_compare (a.ptr, b.ptr) == -1);
59 TEST(mala_string_compare (b.ptr, c.ptr) == 1);
61 TEST(a.ptr = mala_string_new_literal_cstr ("foobar", testbucket));
63 TEST(strcmp (mala_string_cstr (a.ptr), "foobar") == 0);
65 TEST(b.ptr = mala_string_new_attach_cstr (strdup ("foo"), testbucket));
66 TEST(strcmp (mala_string_cstr (b.ptr), "foo") == 0);
68 TEST(c.ptr = mala_string_new_cstr_n ("foobar", 3, testbucket));
69 TEST(c.ptr == b.ptr);
71 TEST(d.ptr = mala_string_new_2cstr ("foo", "bar", testbucket));
72 TEST(d.ptr == a.ptr);
74 TEST(a.ptr = mala_string_new_cstr_n ("foobar", 3, testbucket));
76 TEST(a.ptr = mala_string_new_n (a.ptr, 3, testbucket));
77 TEST(strcmp(mala_string_cstr (a.ptr), "foo") == 0);
79 TEST(b.ptr = mala_string_new_prefix ("bar", a.ptr, testbucket));
80 TEST(c.ptr = mala_string_new_literal_cstr ("barfoo", testbucket));
81 TEST(c.ptr == b.ptr);
83 TEST(onetwothreefour.ptr = mala_string_new_literal_cstr ("1234", testbucket));
84 TEST(mala_string_length (onetwothreefour.ptr) == 4);
86 TEST(d.ptr = mala_string_new_print (testbucket, "%d", 1234));
87 TEST(mala_string_length (d.ptr) == 4);
88 TEST(d.ptr == onetwothreefour.ptr);
89 TEST(mala_string_compare (d.ptr, onetwothreefour.ptr) == 0);
91 TEST(e.ptr = mala_string_new_print (testbucket, "%s", "123456789abcdef longer string"));
92 TEST(mala_string_length (e.ptr) == 29);
94 ACOGC_STACK_LEAVE;
97 void tools (MalaStringBucket testbucket)
99 MalaString a;
101 int i;
102 char abcde[6];
103 float f;
104 char foobar[7];
106 a = mala_string_new_literal_cstr ("1234 abcde 2.25 foobar" , testbucket);
108 TEST(4 == mala_string_scan (a, "%d %s %f %s", &i, abcde, &f, foobar));
110 TEST(i == 1234);
111 TEST(strcmp(abcde, "abcde") == 0);
112 TEST(f == 2.25);
113 TEST(strcmp(foobar, "foobar") == 0);
116 int main()
118 acogc_root_init (&gcroot);
120 acogc_factory_init (&bucketfactory,
121 &gcroot,
122 sizeof(mala_stringbucket),
125 mala_stringbucket_acogc_mark,
126 mala_stringbucket_acogc_initize,
127 NULL,
128 mala_global_type_stringbucket);
130 acogc_factory_init (&gcfactory_strings,
131 &gcroot,
132 sizeof (mala_string),
133 mala_global_stringsfactory_low,
134 mala_global_stringsfactory_high,
135 mala_string_acogc_mark,
136 mala_string_acogc_initize,
137 mala_string_acogc_finalize,
138 mala_global_type_string);
140 acogc_factory_init (&gcfactory_splaynodes,
141 &gcroot,
142 sizeof (mala_stringbucketnode),
143 mala_global_splayfactory_low,
144 mala_global_splayfactory_high,
145 mala_stringbucketnode_acogc_mark,
146 mala_stringbucketnode_acogc_initize,
147 mala_stringbucketnode_acogc_finalize,
148 mala_global_type_stringbucketnode);
151 MalaStringBucket testbucket = mala_stringbucket_new (MALA_STRING_FWD, &bucketfactory, &gcfactory_strings, &gcfactory_splaynodes);
152 acogc_addroot (testbucket);
154 construction (testbucket);
155 tools (testbucket);
157 acogc_root_erase (&gcroot);
158 exit(0);
162 // arch-tag: 99da2844-99b2-4ec3-81ed-32db697656ec