ncdval_test.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * @file ncdval_test.c
  3. * @author Ambroz Bizjak <[email protected]>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <ncd/NCDVal.h>
  31. #include <ncd/NCDStringIndex.h>
  32. #include <ncd/static_strings.h>
  33. #include <base/BLog.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. #include <misc/offset.h>
  37. #define FORCE(cmd) if (!(cmd)) { fprintf(stderr, "failed\n"); exit(1); }
  38. static void test_string (NCDValRef str, const char *data, size_t length)
  39. {
  40. FORCE( !NCDVal_IsInvalid(str) )
  41. FORCE( NCDVal_IsString(str) )
  42. FORCE( NCDVal_StringLength(str) == length )
  43. FORCE( NCDVal_StringHasNulls(str) == !!memchr(data, '\0', length) )
  44. FORCE( NCDVal_IsStringNoNulls(str) == !memchr(data, '\0', length) )
  45. FORCE( NCDVal_StringRegionEquals(str, 0, length, data) )
  46. b_cstring cstr = NCDVal_StringCstring(str);
  47. for (size_t i = 0; i < length; i++) {
  48. size_t chunk_length;
  49. const char *chunk_data = b_cstring_get(cstr, i, length - i, &chunk_length);
  50. FORCE( chunk_length > 0 )
  51. FORCE( chunk_length <= length - i )
  52. FORCE( !memcmp(chunk_data, data + i, chunk_length) )
  53. FORCE( NCDVal_StringRegionEquals(str, i, chunk_length, data + i) )
  54. FORCE( b_cstring_memcmp(cstr, b_cstring_make_buf(data, length), i, i, chunk_length) == 0 )
  55. FORCE( b_cstring_memcmp(cstr, b_cstring_make_buf(data + i, length - i), i, 0, chunk_length) == 0 )
  56. }
  57. }
  58. static void print_indent (int indent)
  59. {
  60. for (int i = 0; i < indent; i++) {
  61. printf(" ");
  62. }
  63. }
  64. static void print_value (NCDValRef val, unsigned int indent)
  65. {
  66. switch (NCDVal_Type(val)) {
  67. case NCDVAL_STRING: {
  68. NCDValNullTermString nts;
  69. FORCE( NCDVal_StringNullTerminate(val, &nts) )
  70. print_indent(indent);
  71. printf("string(%zu) %s\n", NCDVal_StringLength(val), nts.data);
  72. NCDValNullTermString_Free(&nts);
  73. } break;
  74. case NCDVAL_LIST: {
  75. size_t count = NCDVal_ListCount(val);
  76. print_indent(indent);
  77. printf("list(%zu)\n", NCDVal_ListCount(val));
  78. for (size_t i = 0; i < count; i++) {
  79. NCDValRef elem_val = NCDVal_ListGet(val, i);
  80. print_value(elem_val, indent + 1);
  81. }
  82. } break;
  83. case NCDVAL_MAP: {
  84. print_indent(indent);
  85. printf("map(%zu)\n", NCDVal_MapCount(val));
  86. for (NCDValMapElem e = NCDVal_MapOrderedFirst(val); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(val, e)) {
  87. NCDValRef ekey = NCDVal_MapElemKey(val, e);
  88. NCDValRef eval = NCDVal_MapElemVal(val, e);
  89. print_indent(indent + 1);
  90. printf("key=\n");
  91. print_value(ekey, indent + 2);
  92. print_indent(indent + 1);
  93. printf("val=\n");
  94. print_value(eval, indent + 2);
  95. }
  96. } break;
  97. }
  98. }
  99. int main ()
  100. {
  101. int res;
  102. BLog_InitStdout();
  103. NCDStringIndex string_index;
  104. FORCE( NCDStringIndex_Init(&string_index) )
  105. // Some basic usage of values.
  106. NCDValMem mem;
  107. NCDValMem_Init(&mem);
  108. NCDValRef s1 = NCDVal_NewString(&mem, "Hello World");
  109. test_string(s1, "Hello World", 11);
  110. ASSERT( NCDVal_IsString(s1) )
  111. ASSERT( !NCDVal_IsIdString(s1) )
  112. ASSERT( NCDVal_Type(s1) == NCDVAL_STRING )
  113. NCDValRef s2 = NCDVal_NewString(&mem, "This is reeeeeeeeeeeeeallllllllyyyyy fun!");
  114. FORCE( !NCDVal_IsInvalid(s2) )
  115. NCDValRef l1 = NCDVal_NewList(&mem, 10);
  116. FORCE( !NCDVal_IsInvalid(l1) )
  117. FORCE( NCDVal_ListAppend(l1, s1) )
  118. FORCE( NCDVal_ListAppend(l1, s2) )
  119. print_value(s1, 0);
  120. print_value(s2, 0);
  121. print_value(l1, 0);
  122. NCDValRef k1 = NCDVal_NewString(&mem, "K1");
  123. FORCE( !NCDVal_IsInvalid(k1) )
  124. NCDValRef v1 = NCDVal_NewString(&mem, "V1");
  125. FORCE( !NCDVal_IsInvalid(v1) )
  126. NCDValRef k2 = NCDVal_NewString(&mem, "K2");
  127. FORCE( !NCDVal_IsInvalid(k2) )
  128. NCDValRef v2 = NCDVal_NewString(&mem, "V2");
  129. FORCE( !NCDVal_IsInvalid(v2) )
  130. NCDValRef m1 = NCDVal_NewMap(&mem, 3);
  131. FORCE( !NCDVal_IsInvalid(m1) )
  132. FORCE( NCDVal_MapInsert(m1, k1, v1, &res) && res )
  133. FORCE( NCDVal_MapInsert(m1, k2, v2, &res) && res )
  134. ASSERT( NCDVal_MapGetValue(m1, "K1").idx == v1.idx )
  135. ASSERT( NCDVal_MapGetValue(m1, "K2").idx == v2.idx )
  136. ASSERT( NCDVal_IsInvalid(NCDVal_MapGetValue(m1, "K3")) )
  137. NCDValRef ids1 = NCDVal_NewIdString(&mem, NCD_STRING_ARG1, &string_index);
  138. test_string(ids1, "_arg1", 5);
  139. ASSERT( !memcmp(NCDVal_StringData(ids1), "_arg1", 5) )
  140. ASSERT( NCDVal_StringLength(ids1) == 5 )
  141. ASSERT( !NCDVal_StringHasNulls(ids1) )
  142. ASSERT( NCDVal_StringEquals(ids1, "_arg1") )
  143. ASSERT( NCDVal_Type(ids1) == NCDVAL_STRING )
  144. ASSERT( NCDVal_IsIdString(ids1) )
  145. NCDValRef ids2 = NCDVal_NewIdString(&mem, NCD_STRING_ARG2, &string_index);
  146. test_string(ids2, "_arg2", 5);
  147. ASSERT( !memcmp(NCDVal_StringData(ids2), "_arg2", 5) )
  148. ASSERT( NCDVal_StringLength(ids2) == 5 )
  149. ASSERT( !NCDVal_StringHasNulls(ids2) )
  150. ASSERT( NCDVal_StringEquals(ids2, "_arg2") )
  151. ASSERT( NCDVal_Type(ids2) == NCDVAL_STRING )
  152. ASSERT( NCDVal_IsIdString(ids2) )
  153. FORCE( NCDVal_MapInsert(m1, ids1, ids2, &res) && res )
  154. ASSERT( NCDVal_MapGetValue(m1, "_arg1").idx == ids2.idx )
  155. print_value(m1, 0);
  156. NCDValRef copy = NCDVal_NewCopy(&mem, m1);
  157. FORCE( !NCDVal_IsInvalid(copy) )
  158. ASSERT( NCDVal_Compare(copy, m1) == 0 )
  159. NCDValMem_Free(&mem);
  160. // Try to make copies of a string within the same memory object.
  161. // This is an evil test because we cannot simply copy a string using e.g.
  162. // NCDVal_NewStringBin() - it requires that the buffer passed
  163. // be outside the memory object of the new string.
  164. // We use NCDVal_NewCopy(), which takes care of this by creating
  165. // an uninitialized string using NCDVal_NewStringUninitialized() and
  166. // then copyng the data.
  167. NCDValMem_Init(&mem);
  168. NCDValRef s[100];
  169. s[0] = NCDVal_NewString(&mem, "Eeeeeeeeeeeevil.");
  170. FORCE( !NCDVal_IsInvalid(s[0]) )
  171. for (int i = 1; i < 100; i++) {
  172. s[i] = NCDVal_NewCopy(&mem, s[i - 1]);
  173. FORCE( !NCDVal_IsInvalid(s[i]) )
  174. ASSERT( NCDVal_StringEquals(s[i - 1], "Eeeeeeeeeeeevil.") )
  175. ASSERT( NCDVal_StringEquals(s[i], "Eeeeeeeeeeeevil.") )
  176. }
  177. for (int i = 0; i < 100; i++) {
  178. ASSERT( NCDVal_StringEquals(s[i], "Eeeeeeeeeeeevil.") )
  179. }
  180. NCDValMem_Free(&mem);
  181. NCDStringIndex_Free(&string_index);
  182. return 0;
  183. }