Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.6.1.1-b6b19ee
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ValueTest.c
Go to the documentation of this file.
1#include <lal/LALConfig.h>
2
3#include <stdlib.h>
4#include <string.h>
5#include <complex.h>
6#include <lal/LALStdlib.h>
7#include <lal/LALValue.h>
8#include <lal/LALDict.h>
9
10#ifdef __GNUC__
11#define UNUSED __attribute__ ((unused))
12#else
13#define UNUSED
14#endif
15
16#define CHAR_VALUE LAL_INT8_C(+111)
17#define INT2_VALUE LAL_INT8_C(-11111)
18#define INT4_VALUE LAL_INT8_C(-1111111111)
19#define INT8_VALUE LAL_INT8_C(-111111111111111111)
20#define UCHAR_VALUE LAL_UINT8_C(222)
21#define UINT2_VALUE LAL_UINT8_C(44444)
22#define UINT4_VALUE LAL_UINT8_C(3333333333)
23#define UINT8_VALUE LAL_UINT8_C(11111111111111111111)
24#define REAL4_VALUE 100.0
25#define REAL8_VALUE 1e100
26#define COMPLEX8_VALUE 3.0 + 4.0 * I
27#define COMPLEX16_VALUE 3e100 + 4e100 * I
28char BLOB_VALUE[5] = "\x68\x65\x6c\x6c\x6f";
29char String_VALUE[] = "world";
30
31static LALDict * create_dict(void)
32{
33 LALDict *dict;
34 LALDict *intdict;
35 LALDict *floatdict;
36 LALDict *complexdict;
37 LALDict *blobdict;
38 LALDict *strdict;
39 int err = 0;
40 intdict = XLALCreateDict();
41 floatdict = XLALCreateDict();
42 complexdict = XLALCreateDict();
43 blobdict = XLALCreateDict();
44 strdict = XLALCreateDict();
45 if (!intdict || !floatdict || !complexdict || !blobdict || !strdict)
46 return NULL;
47 err |= XLALDictInsertCHARValue(intdict, "CHAR", CHAR_VALUE);
48 err |= XLALDictInsertINT2Value(intdict, "INT2", INT2_VALUE);
49 err |= XLALDictInsertINT4Value(intdict, "INT4", INT4_VALUE);
50 err |= XLALDictInsertINT8Value(intdict, "INT8", INT8_VALUE);
51 err |= XLALDictInsertUCHARValue(intdict, "UCHAR", UCHAR_VALUE);
52 err |= XLALDictInsertUINT2Value(intdict, "UINT2", UINT2_VALUE);
53 err |= XLALDictInsertUINT4Value(intdict, "UINT4", UINT4_VALUE);
54 err |= XLALDictInsertUINT8Value(intdict, "UINT8", UINT8_VALUE);
55 err |= XLALDictInsertREAL4Value(floatdict, "REAL4", REAL4_VALUE);
56 err |= XLALDictInsertREAL8Value(floatdict, "REAL8", REAL8_VALUE);
57 err |= XLALDictInsertCOMPLEX8Value(complexdict, "COMPLEX8", COMPLEX8_VALUE);
58 err |= XLALDictInsertCOMPLEX16Value(complexdict, "COMPLEX16", COMPLEX16_VALUE);
59 err |= XLALDictInsertBLOBValue(blobdict, "BLOB", BLOB_VALUE, sizeof(BLOB_VALUE));
60 err |= XLALDictInsertStringValue(strdict, "String", String_VALUE);
61 dict = XLALDictMerge(intdict, floatdict);
62 XLALDictUpdate(dict, complexdict);
63 XLALDictUpdate(dict, blobdict);
64 XLALDictUpdate(dict, strdict);
65 XLALDestroyDict(strdict);
66 XLALDestroyDict(blobdict);
67 XLALDestroyDict(complexdict);
68 XLALDestroyDict(floatdict);
69 XLALDestroyDict(intdict);
70 return err ? NULL : dict;
71}
72
73static LALList * create_list(void)
74{
75 LALList *list;
76 int err = 0;
77 list = XLALCreateList();
78 if (!list)
79 return NULL;
80 /* put these in alphabetical order */
81 err |= XLALListAddStringValue(list, "BLOB");
82 err |= XLALListAddStringValue(list, "CHAR");
83 err |= XLALListAddStringValue(list, "COMPLEX16");
84 err |= XLALListAddStringValue(list, "COMPLEX8");
85 err |= XLALListAddStringValue(list, "INT2");
86 err |= XLALListAddStringValue(list, "INT4");
87 err |= XLALListAddStringValue(list, "INT8");
88 err |= XLALListAddStringValue(list, "REAL4");
89 err |= XLALListAddStringValue(list, "REAL8");
90 err |= XLALListAddStringValue(list, "String");
91 err |= XLALListAddStringValue(list, "UCHAR");
92 err |= XLALListAddStringValue(list, "UINT2");
93 err |= XLALListAddStringValue(list, "UINT4");
94 err |= XLALListAddStringValue(list, "UINT8");
95 /* inserting them in this order puts them in reverse order... */
96 XLALListReverse(list);
97 return err ? NULL : list;
98}
99
100static int lists_are_equal(LALList *list1, LALList *list2)
101{
102 LALListIter iter1;
103 LALListIter iter2;
104 LALListItem *item1;
105 LALListItem *item2;
106 if (XLALListSize(list1) != XLALListSize(list2))
107 return 0;
108 XLALListIterInit(&iter1, list1);
109 XLALListIterInit(&iter2, list2);
110 while ((item1 = XLALListIterNext(&iter1)) && (item2 = XLALListIterNext(&iter2))) {
111 const LALValue *value1 = XLALListItemGetValue(item1);
112 const LALValue *value2 = XLALListItemGetValue(item2);
113 if (!XLALValueEqual(value1, value2))
114 return 0;
115 }
116 return 1;
117}
118
119static int string_value_cmp(const LALValue *value1, const LALValue *value2, void UNUSED *thunk)
120{
121 return strcmp(XLALValueGetString(value1), XLALValueGetString(value2));
122}
123
124#define COMPARE(v, TYPE) (v == TYPE ## _VALUE)
125
126#define TEST(TYPE) \
127 fprintf(stderr, "Testing %s... ", #TYPE); \
128 entry = XLALDictLookup(dict, #TYPE); \
129 orig = XLALDictEntryGetValue(entry); \
130 size = XLALValueGetSize(orig); \
131 copy = XLALValueRealloc(copy, size); \
132 copy = XLALValueCopy(copy, orig); \
133 if (!XLALValueEqual(copy, orig)) { \
134 fprintf(stderr, "failed:"); \
135 XLALValuePrint(copy, 2); \
136 fprintf(stderr, " != "); \
137 XLALValuePrint(orig, 2); \
138 fprintf(stderr, "\n"); \
139 return 1; \
140 } \
141 if (!COMPARE(XLALValueGet ## TYPE(copy), TYPE)) { \
142 fprintf(stderr, "failed: incorrect value\n"); \
143 return 1; \
144 } \
145 XLALDictRemove(dict, #TYPE); \
146 fprintf(stderr, "passed\n");
147
148#define TEST2(TYPE) \
149 fprintf(stderr, "Testing %s Pop... ", #TYPE); \
150 if (!COMPARE(XLALDictPop ## TYPE ## Value(dict2, #TYPE), TYPE)) { \
151 fprintf(stderr, "failed: incorrect value\n"); \
152 return 1; \
153 } \
154 fprintf(stderr, "passed\n");
155
156int main(void)
157{
158 LALDict *dict;
159 LALDict *dict2;
160 LALList *list;
161 LALList *keys;
162
163 LALDictEntry *entry;
164 const LALValue *orig;
165 LALValue *copy = NULL;
166 size_t size;
167 void *blob;
168 char *str;
169
170 /* make sure that the keys in the dict are what they should be */
171 list = create_list();
172 dict = create_dict();
173 keys = XLALDictKeys(dict);
174 XLALListSort(keys, string_value_cmp, NULL);
175 if (!lists_are_equal(list, keys))
176 return 1;
177
178 dict2 = XLALDictDuplicate(dict);
179
180 /* make sure the values in the dict are what they should be */
181 TEST(CHAR)
182 TEST2(CHAR)
183 TEST(INT2)
184 TEST2(INT2)
185 TEST(INT4)
186 TEST2(INT4)
187 TEST(INT8)
188 TEST2(INT8)
189 TEST(UCHAR)
190 TEST2(UCHAR)
191 TEST(UINT2)
192 TEST2(UINT2)
193 TEST(UINT4)
194 TEST2(UINT4)
195 TEST(UINT8)
196 TEST2(UINT8)
197 TEST(REAL4)
198 TEST2(REAL4)
199 TEST(REAL8)
200 TEST2(REAL8)
205
206#undef COMPARE
207#define COMPARE(v, TYPE) (!memcmp(blob = v, BLOB_VALUE, sizeof(BLOB_VALUE)))
208 TEST(BLOB)
209 LALFree(blob);
210 TEST2(BLOB)
211 LALFree(blob);
212
213#undef COMPARE
214#define COMPARE(v, TYPE) (!strcmp(v, String_VALUE))
215 TEST(String)
216#undef COMPARE
217#define COMPARE(v, TYPE) (!strcmp(str = v, String_VALUE))
218 TEST2(String)
219 LALFree(str);
220
221 /* dict should now be empty */
222 if (XLALDictSize(dict) != 0) {
223 fprintf(stderr, "Error: Dictionary has %zu leftover elements\n", XLALDictSize(dict));
224 return 1;
225 }
226
227 /* dict2 should now be empty */
228 if (XLALDictSize(dict2) != 0) {
229 fprintf(stderr, "Error: Dictionary has %zu leftover elements\n", XLALDictSize(dict2));
230 return 1;
231 }
232
233 XLALDestroyDict(dict2);
234 XLALDestroyDict(dict);
235 XLALDestroyList(keys);
236 XLALDestroyList(list);
237 XLALDestroyValue(copy);
238
240 return 0;
241}
LALList * XLALDictKeys(const LALDict *dict)
Definition: LALDict.c:244
int XLALDictUpdate(LALDict *dst, const LALDict *src)
Definition: LALDict.c:208
LALDict * XLALDictMerge(const LALDict *dict1, const LALDict *dict2)
Definition: LALDict.c:225
void XLALDestroyDict(LALDict *dict)
Definition: LALDict.c:137
LALDict * XLALDictDuplicate(const LALDict *orig)
Definition: LALDict.c:239
LALDict * XLALCreateDict(void)
Definition: LALDict.c:148
int XLALDictInsertBLOBValue(LALDict *dict, const char *key, const void *blob, size_t size)
Definition: LALDict.c:404
size_t XLALDictSize(const LALDict *dict)
Definition: LALDict.c:293
int XLALDictInsertStringValue(LALDict *dict, const char *key, const char *string)
Definition: LALDict.c:411
int XLALDictInsertUINT4Value(LALDict *dict, const char *key, UINT4 value)
int XLALDictInsertUINT2Value(LALDict *dict, const char *key, UINT2 value)
int XLALDictInsertINT2Value(LALDict *dict, const char *key, INT2 value)
int XLALDictInsertINT8Value(LALDict *dict, const char *key, INT8 value)
int XLALDictInsertCOMPLEX16Value(LALDict *dict, const char *key, COMPLEX16 value)
int XLALDictInsertINT4Value(LALDict *dict, const char *key, INT4 value)
int XLALDictInsertREAL8Value(LALDict *dict, const char *key, REAL8 value)
int XLALDictInsertCOMPLEX8Value(LALDict *dict, const char *key, COMPLEX8 value)
int XLALDictInsertUINT8Value(LALDict *dict, const char *key, UINT8 value)
int XLALDictInsertUCHARValue(LALDict *dict, const char *key, UCHAR value)
int XLALDictInsertREAL4Value(LALDict *dict, const char *key, REAL4 value)
int XLALDictInsertCHARValue(LALDict *dict, const char *key, CHAR value)
int XLALListReverse(LALList *list)
Definition: LALList.c:198
size_t XLALListSize(const LALList *list)
Definition: LALList.c:268
LALList * XLALCreateList(void)
Definition: LALList.c:169
void XLALListIterInit(LALListIter *iter, LALList *list)
Definition: LALList.c:475
int XLALListSort(LALList *list, int(*cmp)(const LALValue *, const LALValue *, void *), void *thunk)
Definition: LALList.c:233
int XLALListAddStringValue(LALList *list, const char *string)
Definition: LALList.c:522
const LALValue * XLALListItemGetValue(const LALListItem *item)
Definition: LALList.c:83
LALListItem * XLALListIterNext(LALListIter *iter)
Definition: LALList.c:480
void XLALDestroyList(LALList *list)
Definition: LALList.c:155
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
#define LALFree(p)
Definition: LALMalloc.h:96
int XLALValueEqual(const LALValue *value1, const LALValue *value2)
Definition: LALValue.c:203
void XLALDestroyValue(LALValue *value)
Definition: LALValue.c:122
const char * XLALValueGetString(const LALValue *value)
Definition: LALValue.c:223
#define fprintf
#define TEST2(TYPE)
Definition: ValueTest.c:148
#define COMPLEX16_VALUE
Definition: ValueTest.c:27
#define CHAR_VALUE
Definition: ValueTest.c:16
static int lists_are_equal(LALList *list1, LALList *list2)
Definition: ValueTest.c:100
static LALDict * create_dict(void)
Definition: ValueTest.c:31
static LALList * create_list(void)
Definition: ValueTest.c:73
#define UINT2_VALUE
Definition: ValueTest.c:21
char String_VALUE[]
Definition: ValueTest.c:29
#define COMPLEX8_VALUE
Definition: ValueTest.c:26
#define REAL8_VALUE
Definition: ValueTest.c:25
#define INT8_VALUE
Definition: ValueTest.c:19
static int string_value_cmp(const LALValue *value1, const LALValue *value2, void UNUSED *thunk)
Definition: ValueTest.c:119
#define UINT8_VALUE
Definition: ValueTest.c:23
int main(void)
Definition: ValueTest.c:156
#define TEST(TYPE)
Definition: ValueTest.c:126
#define UINT4_VALUE
Definition: ValueTest.c:22
#define INT4_VALUE
Definition: ValueTest.c:18
char BLOB_VALUE[5]
Definition: ValueTest.c:28
#define UCHAR_VALUE
Definition: ValueTest.c:20
#define REAL4_VALUE
Definition: ValueTest.c:24
#define INT2_VALUE
Definition: ValueTest.c:17
unsigned char UCHAR
One-byte unsigned integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
double complex COMPLEX16
Double-precision floating-point complex number (16 bytes total)
double REAL8
Double precision real floating-point number (8 bytes).
int16_t INT2
Two-byte signed integer.
int64_t INT8
Eight-byte signed integer; on some platforms this is equivalent to long int instead.
uint16_t UINT2
Two-byte unsigned integer.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
float complex COMPLEX8
Single-precision floating-point complex number (8 bytes total)
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).