ncdvalcons_test.c 3.6 KB

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