ncdval_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file ncdval_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  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. #define FORCE(cmd) if (!(cmd)) { fprintf(stderr, "failed\n"); exit(1); }
  36. static void print_indent (int indent)
  37. {
  38. for (int i = 0; i < indent; i++) {
  39. printf(" ");
  40. }
  41. }
  42. static void print_value (NCDValRef val, unsigned int indent)
  43. {
  44. switch (NCDVal_Type(val)) {
  45. case NCDVAL_STRING: {
  46. print_indent(indent);
  47. printf("string(%zu) %s\n", NCDVal_StringLength(val), NCDVal_StringValue(val));
  48. } break;
  49. case NCDVAL_LIST: {
  50. size_t count = NCDVal_ListCount(val);
  51. print_indent(indent);
  52. printf("list(%zu)\n", NCDVal_ListCount(val));
  53. for (size_t i = 0; i < count; i++) {
  54. NCDValRef elem_val = NCDVal_ListGet(val, i);
  55. print_value(elem_val, indent + 1);
  56. }
  57. } break;
  58. case NCDVAL_MAP: {
  59. print_indent(indent);
  60. printf("map(%zu)\n", NCDVal_MapCount(val));
  61. for (NCDValMapElem e = NCDVal_MapOrderedFirst(val); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(val, e)) {
  62. NCDValRef ekey = NCDVal_MapElemKey(val, e);
  63. NCDValRef eval = NCDVal_MapElemVal(val, e);
  64. print_indent(indent + 1);
  65. printf("key=\n");
  66. print_value(ekey, indent + 2);
  67. print_indent(indent + 1);
  68. printf("val=\n");
  69. print_value(eval, indent + 2);
  70. }
  71. } break;
  72. }
  73. }
  74. int main ()
  75. {
  76. BLog_InitStdout();
  77. NCDStringIndex string_index;
  78. FORCE( NCDStringIndex_Init(&string_index) )
  79. // Some basic usage of values.
  80. NCDValMem mem;
  81. NCDValMem_Init(&mem);
  82. NCDValRef s1 = NCDVal_NewString(&mem, "Hello World");
  83. FORCE( !NCDVal_IsInvalid(s1) )
  84. ASSERT( NCDVal_IsString(s1) )
  85. ASSERT( !NCDVal_IsIdString(s1) )
  86. ASSERT( NCDVal_Type(s1) == NCDVAL_STRING )
  87. NCDValRef s2 = NCDVal_NewString(&mem, "This is reeeeeeeeeeeeeallllllllyyyyy fun!");
  88. FORCE( !NCDVal_IsInvalid(s2) )
  89. printf("%s. %s\n", NCDVal_StringValue(s1), NCDVal_StringValue(s2));
  90. NCDValRef l1 = NCDVal_NewList(&mem, 10);
  91. FORCE( !NCDVal_IsInvalid(l1) )
  92. NCDVal_ListAppend(l1, s1);
  93. NCDVal_ListAppend(l1, s2);
  94. print_value(s1, 0);
  95. print_value(s2, 0);
  96. print_value(l1, 0);
  97. NCDValRef k1 = NCDVal_NewString(&mem, "K1");
  98. FORCE( !NCDVal_IsInvalid(k1) )
  99. NCDValRef v1 = NCDVal_NewString(&mem, "V1");
  100. FORCE( !NCDVal_IsInvalid(v1) )
  101. NCDValRef k2 = NCDVal_NewString(&mem, "K2");
  102. FORCE( !NCDVal_IsInvalid(k2) )
  103. NCDValRef v2 = NCDVal_NewString(&mem, "V2");
  104. FORCE( !NCDVal_IsInvalid(v2) )
  105. NCDValRef m1 = NCDVal_NewMap(&mem, 3);
  106. FORCE( !NCDVal_IsInvalid(m1) )
  107. FORCE( NCDVal_MapInsert(m1, k1, v1) )
  108. FORCE( NCDVal_MapInsert(m1, k2, v2) )
  109. NCDValRef ids1 = NCDVal_NewIdString(&mem, NCD_STRING_ARG1, &string_index);
  110. FORCE( !NCDVal_IsInvalid(ids1) )
  111. ASSERT( !strcmp(NCDVal_StringValue(ids1), "_arg1") )
  112. ASSERT( NCDVal_StringLength(ids1) == 5 )
  113. ASSERT( !NCDVal_StringHasNulls(ids1) )
  114. ASSERT( NCDVal_StringEquals(ids1, "_arg1") )
  115. ASSERT( NCDVal_Type(ids1) == NCDVAL_STRING )
  116. ASSERT( NCDVal_IsIdString(ids1) )
  117. NCDValRef ids2 = NCDVal_NewIdString(&mem, NCD_STRING_ARG2, &string_index);
  118. FORCE( !NCDVal_IsInvalid(ids2) )
  119. ASSERT( !strcmp(NCDVal_StringValue(ids2), "_arg2") )
  120. ASSERT( NCDVal_StringLength(ids2) == 5 )
  121. ASSERT( !NCDVal_StringHasNulls(ids2) )
  122. ASSERT( NCDVal_StringEquals(ids2, "_arg2") )
  123. ASSERT( NCDVal_Type(ids2) == NCDVAL_STRING )
  124. ASSERT( NCDVal_IsIdString(ids2) )
  125. FORCE( NCDVal_MapInsert(m1, ids1, ids2) )
  126. print_value(m1, 0);
  127. NCDValRef copy = NCDVal_NewCopy(&mem, m1);
  128. FORCE( !NCDVal_IsInvalid(copy) )
  129. ASSERT( NCDVal_Compare(copy, m1) == 0 )
  130. NCDValMem_Free(&mem);
  131. // Try to make copies of a string within the same memory object.
  132. // This is an evil test because we cannot simply copy a string using e.g.
  133. // NCDVal_NewStringBin() - it requires that the buffer passed
  134. // be outside the memory object of the new string.
  135. // We use NCDVal_NewCopy(), which takes care of this by creating
  136. // an uninitialized string using NCDVal_NewStringUninitialized() and
  137. // then copyng the data.
  138. NCDValMem_Init(&mem);
  139. NCDValRef s[100];
  140. s[0] = NCDVal_NewString(&mem, "Eeeeeeeeeeeevil.");
  141. FORCE( !NCDVal_IsInvalid(s[0]) )
  142. for (int i = 1; i < 100; i++) {
  143. s[i] = NCDVal_NewCopy(&mem, s[i - 1]);
  144. FORCE( !NCDVal_IsInvalid(s[i]) )
  145. ASSERT( !strcmp(NCDVal_StringValue(s[i - 1]), "Eeeeeeeeeeeevil.") )
  146. ASSERT( !strcmp(NCDVal_StringValue(s[i]), "Eeeeeeeeeeeevil.") )
  147. }
  148. for (int i = 0; i < 100; i++) {
  149. ASSERT( !strcmp(NCDVal_StringValue(s[i]), "Eeeeeeeeeeeevil.") )
  150. }
  151. NCDValMem_Free(&mem);
  152. NCDStringIndex_Free(&string_index);
  153. return 0;
  154. }