ncdval_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <base/BLog.h>
  32. #include <misc/debug.h>
  33. #define FORCE(cmd) if (!(cmd)) { fprintf(stderr, "failed\n"); exit(1); }
  34. static void print_indent (int indent)
  35. {
  36. for (int i = 0; i < indent; i++) {
  37. printf(" ");
  38. }
  39. }
  40. static void print_value (NCDValRef val, unsigned int indent)
  41. {
  42. switch (NCDVal_Type(val)) {
  43. case NCDVAL_STRING: {
  44. print_indent(indent);
  45. printf("string(%zu) %s\n", NCDVal_StringLength(val), NCDVal_StringValue(val));
  46. } break;
  47. case NCDVAL_LIST: {
  48. size_t count = NCDVal_ListCount(val);
  49. print_indent(indent);
  50. printf("list(%zu)\n", NCDVal_ListCount(val));
  51. for (size_t i = 0; i < count; i++) {
  52. NCDValRef elem_val = NCDVal_ListGet(val, i);
  53. print_value(elem_val, indent + 1);
  54. }
  55. } break;
  56. case NCDVAL_MAP: {
  57. print_indent(indent);
  58. printf("map(%zu)\n", NCDVal_MapCount(val));
  59. for (NCDValMapElem e = NCDVal_MapOrderedFirst(val); !NCDVal_MapElemInvalid(e); e = NCDVal_MapOrderedNext(val, e)) {
  60. NCDValRef ekey = NCDVal_MapElemKey(val, e);
  61. NCDValRef eval = NCDVal_MapElemVal(val, e);
  62. print_indent(indent + 1);
  63. printf("key=\n");
  64. print_value(ekey, indent + 2);
  65. print_indent(indent + 1);
  66. printf("val=\n");
  67. print_value(eval, indent + 2);
  68. }
  69. } break;
  70. }
  71. }
  72. int main ()
  73. {
  74. BLog_InitStdout();
  75. // Some basic usage of values.
  76. NCDValMem mem;
  77. NCDValMem_Init(&mem);
  78. NCDValRef s1 = NCDVal_NewString(&mem, "Hello World");
  79. FORCE( !NCDVal_IsInvalid(s1) )
  80. NCDValRef s2 = NCDVal_NewString(&mem, "This is reeeeeeeeeeeeeallllllllyyyyy fun!");
  81. FORCE( !NCDVal_IsInvalid(s2) )
  82. printf("%s. %s\n", NCDVal_StringValue(s1), NCDVal_StringValue(s2));
  83. NCDValRef l1 = NCDVal_NewList(&mem, 10);
  84. FORCE( !NCDVal_IsInvalid(l1) )
  85. NCDVal_ListAppend(l1, s1);
  86. NCDVal_ListAppend(l1, s2);
  87. print_value(s1, 0);
  88. print_value(s2, 0);
  89. print_value(l1, 0);
  90. NCDValRef k1 = NCDVal_NewString(&mem, "K1");
  91. FORCE( !NCDVal_IsInvalid(k1) )
  92. NCDValRef v1 = NCDVal_NewString(&mem, "V1");
  93. FORCE( !NCDVal_IsInvalid(v1) )
  94. NCDValRef k2 = NCDVal_NewString(&mem, "K2");
  95. FORCE( !NCDVal_IsInvalid(k2) )
  96. NCDValRef v2 = NCDVal_NewString(&mem, "V2");
  97. FORCE( !NCDVal_IsInvalid(v2) )
  98. NCDValRef m1 = NCDVal_NewMap(&mem, 2);
  99. FORCE( !NCDVal_IsInvalid(m1) )
  100. FORCE( NCDVal_MapInsert(m1, k1, v1) )
  101. FORCE( NCDVal_MapInsert(m1, k2, v2) )
  102. print_value(m1, 0);
  103. NCDValMem_Free(&mem);
  104. // Try to make copies of a string within the same memory object.
  105. // This is an evil test because we cannot simply copy a string using e.g.
  106. // NCDVal_NewStringBin() - it requires that the buffer passed
  107. // be outside the memory object of the new string.
  108. // We use NCDVal_NewCopy(), which takes care of this by creating
  109. // an uninitialized string using NCDVal_NewStringUninitialized() and
  110. // then copyng the data.
  111. NCDValMem_Init(&mem);
  112. NCDValRef s[100];
  113. s[0] = NCDVal_NewString(&mem, "Eeeeeeeeeeeevil.");
  114. FORCE( !NCDVal_IsInvalid(s[0]) )
  115. for (int i = 1; i < 100; i++) {
  116. s[i] = NCDVal_NewCopy(&mem, s[i - 1]);
  117. FORCE( !NCDVal_IsInvalid(s[i]) )
  118. ASSERT( !strcmp(NCDVal_StringValue(s[i - 1]), "Eeeeeeeeeeeevil.") )
  119. ASSERT( !strcmp(NCDVal_StringValue(s[i]), "Eeeeeeeeeeeevil.") )
  120. }
  121. for (int i = 0; i < 100; i++) {
  122. ASSERT( !strcmp(NCDVal_StringValue(s[i]), "Eeeeeeeeeeeevil.") )
  123. }
  124. NCDValMem_Free(&mem);
  125. return 0;
  126. }