LAL  7.5.0.1-89842e6
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
28 char BLOB_VALUE[5] = "\x68\x65\x6c\x6c\x6f";
29 char String_VALUE[] = "world";
30 
31 static LALDict * create_dict(void)
32 {
33  LALDict *dict;
34  int err = 0;
35  dict = XLALCreateDict();
36  if (!dict)
37  return NULL;
38  err |= XLALDictInsertCHARValue(dict, "CHAR", CHAR_VALUE);
39  err |= XLALDictInsertINT2Value(dict, "INT2", INT2_VALUE);
40  err |= XLALDictInsertINT4Value(dict, "INT4", INT4_VALUE);
41  err |= XLALDictInsertINT8Value(dict, "INT8", INT8_VALUE);
42  err |= XLALDictInsertUCHARValue(dict, "UCHAR", UCHAR_VALUE);
43  err |= XLALDictInsertUINT2Value(dict, "UINT2", UINT2_VALUE);
44  err |= XLALDictInsertUINT4Value(dict, "UINT4", UINT4_VALUE);
45  err |= XLALDictInsertUINT8Value(dict, "UINT8", UINT8_VALUE);
46  err |= XLALDictInsertREAL4Value(dict, "REAL4", REAL4_VALUE);
47  err |= XLALDictInsertREAL8Value(dict, "REAL8", REAL8_VALUE);
48  err |= XLALDictInsertCOMPLEX8Value(dict, "COMPLEX8", COMPLEX8_VALUE);
49  err |= XLALDictInsertCOMPLEX16Value(dict, "COMPLEX16", COMPLEX16_VALUE);
50  err |= XLALDictInsertBLOBValue(dict, "BLOB", BLOB_VALUE, sizeof(BLOB_VALUE));
51  err |= XLALDictInsertStringValue(dict, "String", String_VALUE);
52  return err ? NULL : dict;
53 }
54 
55 static LALList * create_list(void)
56 {
57  LALList *list;
58  int err = 0;
59  list = XLALCreateList();
60  if (!list)
61  return NULL;
62  /* put these in alphabetical order */
63  err |= XLALListAddStringValue(list, "BLOB");
64  err |= XLALListAddStringValue(list, "CHAR");
65  err |= XLALListAddStringValue(list, "COMPLEX16");
66  err |= XLALListAddStringValue(list, "COMPLEX8");
67  err |= XLALListAddStringValue(list, "INT2");
68  err |= XLALListAddStringValue(list, "INT4");
69  err |= XLALListAddStringValue(list, "INT8");
70  err |= XLALListAddStringValue(list, "REAL4");
71  err |= XLALListAddStringValue(list, "REAL8");
72  err |= XLALListAddStringValue(list, "String");
73  err |= XLALListAddStringValue(list, "UCHAR");
74  err |= XLALListAddStringValue(list, "UINT2");
75  err |= XLALListAddStringValue(list, "UINT4");
76  err |= XLALListAddStringValue(list, "UINT8");
77  /* inserting them in this order puts them in reverse order... */
78  XLALListReverse(list);
79  return err ? NULL : list;
80 }
81 
82 static int lists_are_equal(LALList *list1, LALList *list2)
83 {
84  LALListIter iter1;
85  LALListIter iter2;
86  LALListItem *item1;
87  LALListItem *item2;
88  if (XLALListSize(list1) != XLALListSize(list2))
89  return 0;
90  XLALListIterInit(&iter1, list1);
91  XLALListIterInit(&iter2, list2);
92  while ((item1 = XLALListIterNext(&iter1)) && (item2 = XLALListIterNext(&iter2))) {
93  const LALValue *value1 = XLALListItemGetValue(item1);
94  const LALValue *value2 = XLALListItemGetValue(item2);
95  if (!XLALValueEqual(value1, value2))
96  return 0;
97  }
98  return 1;
99 }
100 
101 static int string_value_cmp(const LALValue *value1, const LALValue *value2, void UNUSED *thunk)
102 {
103  return strcmp(XLALValueGetString(value1), XLALValueGetString(value2));
104 }
105 
106 #define COMPARE(v, TYPE) (v == TYPE ## _VALUE)
107 
108 #define TEST(TYPE) \
109  fprintf(stderr, "Testing %s...", #TYPE); \
110  entry = XLALDictLookup(dict, #TYPE); \
111  orig = XLALDictEntryGetValue(entry); \
112  size = XLALValueGetSize(orig); \
113  copy = XLALValueRealloc(copy, size); \
114  copy = XLALValueCopy(copy, orig); \
115  if (!XLALValueEqual(copy, orig)) \
116  return 1; \
117  if (!COMPARE(XLALValueGet ## TYPE(copy), TYPE)) \
118  return 1; \
119  XLALDictRemove(dict, #TYPE); \
120  fprintf(stderr, " passed\n");
121 
122 int main(void)
123 {
124  LALDict *dict;
125  LALList *list;
126  LALList *keys;
127 
128  LALDictEntry *entry;
129  const LALValue *orig;
130  LALValue *copy = NULL;
131  size_t size;
132  void *blob;
133 
134  /* make sure that the keys in the dict are what they should be */
135  list = create_list();
136  dict = create_dict();
137  keys = XLALDictKeys(dict);
138  XLALListSort(keys, string_value_cmp, NULL);
139  if (!lists_are_equal(list, keys))
140  return 1;
141 
142  /* make sure the values in the dict are what they should be */
143  TEST(CHAR)
144  TEST(INT2)
145  TEST(INT4)
146  TEST(INT8)
147  TEST(UCHAR)
148  TEST(UINT2)
149  TEST(UINT4)
150  TEST(UINT8)
151  TEST(REAL4)
152  TEST(REAL8)
153  TEST(COMPLEX8)
154  TEST(COMPLEX16)
155 
156 #undef COMPARE
157 #define COMPARE(v, TYPE) (!memcmp(blob = v, BLOB_VALUE, sizeof(BLOB_VALUE)))
158  TEST(BLOB)
159  LALFree(blob);
160 
161 #undef COMPARE
162 #define COMPARE(v, TYPE) (!strcmp(v, String_VALUE))
163  TEST(String)
164 
165  /* dict should now be empty */
166  if (XLALDictSize(dict) != 0)
167  return 1;
168 
169  XLALDestroyDict(dict);
170  XLALDestroyList(keys);
171  XLALDestroyList(list);
172  XLALDestroyValue(copy);
173 
175  return 0;
176 }
LALList * XLALDictKeys(const LALDict *dict)
Definition: LALDict.c:221
void XLALDestroyDict(LALDict *dict)
Definition: LALDict.c:127
int XLALDictInsertBLOBValue(LALDict *dict, const char *key, const void *blob, size_t size)
Definition: LALDict.c:373
size_t XLALDictSize(const LALDict *dict)
Definition: LALDict.c:270
int XLALDictInsertStringValue(LALDict *dict, const char *key, const char *string)
Definition: LALDict.c:380
LALDict * XLALCreateDict(void)
Definition: LALDict.c:138
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
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
void XLALDestroyList(LALList *list)
Definition: LALList.c:155
LALListItem * XLALListIterNext(LALListIter *iter)
Definition: LALList.c:480
LALList * XLALCreateList(void)
Definition: LALList.c:169
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 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:82
#define UINT2_VALUE
Definition: ValueTest.c:21
static LALList * create_list(void)
Definition: ValueTest.c:55
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:101
#define UINT8_VALUE
Definition: ValueTest.c:23
int main(void)
Definition: ValueTest.c:122
#define TEST(TYPE)
Definition: ValueTest.c:108
static LALDict * create_dict(void)
Definition: ValueTest.c:31
#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).