ncdvalcons_test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @file ncdvalcons_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 <string.h>
  30. #include <stdio.h>
  31. #include <misc/debug.h>
  32. #include <ncd/NCDValCons.h>
  33. #include <ncd/NCDValGenerator.h>
  34. static NCDStringIndex string_index;
  35. static NCDValMem mem;
  36. static NCDValCons cons;
  37. static NCDValConsVal make_string (const char *data)
  38. {
  39. NCDValConsVal val;
  40. int error;
  41. int res = NCDValCons_NewString(&cons, (const uint8_t *)data, strlen(data), &val, &error);
  42. ASSERT_FORCE(res)
  43. return val;
  44. }
  45. static NCDValConsVal make_list (void)
  46. {
  47. NCDValConsVal val;
  48. NCDValCons_NewList(&cons, &val);
  49. return val;
  50. }
  51. static NCDValConsVal make_map (void)
  52. {
  53. NCDValConsVal val;
  54. NCDValCons_NewMap(&cons, &val);
  55. return val;
  56. }
  57. static NCDValConsVal list_prepend (NCDValConsVal list, NCDValConsVal elem)
  58. {
  59. int error;
  60. int res = NCDValCons_ListPrepend(&cons, &list, elem, &error);
  61. ASSERT_FORCE(res)
  62. return list;
  63. }
  64. static NCDValConsVal map_insert (NCDValConsVal map, NCDValConsVal key, NCDValConsVal value)
  65. {
  66. int error;
  67. int res = NCDValCons_MapInsert(&cons, &map, key, value, &error);
  68. ASSERT_FORCE(res)
  69. return map;
  70. }
  71. static NCDValRef complete (NCDValConsVal cval)
  72. {
  73. int error;
  74. NCDValRef val;
  75. int res = NCDValCons_Complete(&cons, cval, &val, &error);
  76. ASSERT_FORCE(res)
  77. return val;
  78. }
  79. int main ()
  80. {
  81. int res;
  82. res = NCDStringIndex_Init(&string_index);
  83. ASSERT_FORCE(res)
  84. NCDValMem_Init(&mem, &string_index);
  85. res = NCDValCons_Init(&cons, &mem);
  86. ASSERT_FORCE(res)
  87. NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list()));
  88. char *str1 = NCDValGenerator_Generate(val1);
  89. ASSERT_FORCE(str1)
  90. ASSERT_FORCE(!strcmp(str1, "{{}, \"world\", \"hello\"}"))
  91. free(str1);
  92. NCDValRef val2 = complete(map_insert(map_insert(map_insert(make_map(), make_list(), make_list()), make_string("A"), make_list()), make_string("B"), make_list()));
  93. char *str2 = NCDValGenerator_Generate(val2);
  94. ASSERT_FORCE(str2)
  95. printf("%s\n", str2);
  96. ASSERT_FORCE(!strcmp(str2, "[\"A\":{}, \"B\":{}, {}:{}]"))
  97. free(str2);
  98. NCDValCons_Free(&cons);
  99. NCDValMem_Free(&mem);
  100. NCDStringIndex_Free(&string_index);
  101. return 0;
  102. }