summaryrefslogtreecommitdiff
path: root/testsuite/sexp-test.c
blob: 324d61f0fc0568518fae68422d5928ae3a17de7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "testutils.h"
#include "sexp.h"

void
test_main(void)
{
  struct sexp_iterator i;
  uint32_t x;
  
  ASSERT(sexp_iterator_first(&i, LDATA("")));
  ASSERT(i.type == SEXP_END);

  ASSERT(sexp_iterator_first(&i, LDATA("()")));
  ASSERT(i.type == SEXP_LIST
	 && sexp_iterator_enter_list(&i)
	 && i.type == SEXP_END
	 && sexp_iterator_exit_list(&i)
	 && i.type == SEXP_END);

  ASSERT(sexp_iterator_first(&i, LDATA("(")));
  ASSERT(i.type == SEXP_LIST
	 && !sexp_iterator_enter_list(&i));

  /* Check integers. */
  ASSERT(sexp_iterator_first(&i, LDATA("1:\0"
				       "1:\x11"
				       "2:\x00\x11"
				       "2:\x00\x80"
				       "5:\x00\xaa\xbb\xcc\xdd")));
  ASSERT(sexp_iterator_get_uint32(&i, &x) && x == 0);
  ASSERT(sexp_iterator_get_uint32(&i, &x) && x == 0x11);
  ASSERT(sexp_iterator_get_uint32(&i, &x) && x == 0x11);
  ASSERT(sexp_iterator_get_uint32(&i, &x) && x == 0x80);
  ASSERT(sexp_iterator_get_uint32(&i, &x) && x == 0xaabbccdd);

  ASSERT(sexp_iterator_first(&i, LDATA("3:foo0:[3:bar]12:xxxxxxxxxxxx")));
  ASSERT(i.type == SEXP_ATOM
	 && !i.display_length && !i.display
	 && i.atom_length == 3 && MEMEQ(3, "foo", i.atom)

	 && sexp_iterator_next(&i) && i.type == SEXP_ATOM
	 && !i.display_length && !i.display
	 && !i.atom_length && i.atom

	 && sexp_iterator_next(&i) && i.type == SEXP_ATOM
	 && i.display_length == 3 && MEMEQ(3, "bar", i.display)
	 && i.atom_length == 12 && MEMEQ(12, "xxxxxxxxxxxx", i.atom)

	 && sexp_iterator_next(&i) && i.type == SEXP_END);
  
  /* Same data, transport encoded. */

  {
    struct tstring *s
      = tstring_data(LDATA("{Mzpmb28=} {MDo=} {WzM6YmFyXTEyOnh4eHh4eHh4eHh4eA==}"));
  ASSERT(sexp_transport_iterator_first (&i, s->length, s->data));
  ASSERT(i.type == SEXP_ATOM
	 && !i.display_length && !i.display
	 && i.atom_length == 3 && MEMEQ(3, "foo", i.atom)

	 && sexp_iterator_next(&i) && i.type == SEXP_ATOM
	 && !i.display_length && !i.display
	 && !i.atom_length && i.atom

	 && sexp_iterator_next(&i) && i.type == SEXP_ATOM
	 && i.display_length == 3 && MEMEQ(3, "bar", i.display)
	 && i.atom_length == 12 && MEMEQ(12, "xxxxxxxxxxxx", i.atom)

	 && sexp_iterator_next(&i) && i.type == SEXP_END);

  }
  {
    static const uint8_t *keys[2] = { "n", "e" };
    struct sexp_iterator v[2];
    
    ASSERT(sexp_iterator_first(&i, LDATA("((1:n2:xx3:foo)0:(1:y)(1:e))")));
    ASSERT(sexp_iterator_enter_list(&i)
	   && sexp_iterator_assoc(&i, 2, keys, v));

    ASSERT(v[0].type == SEXP_ATOM
	   && !v[0].display_length && !v[0].display
	   && v[0].atom_length == 2 && MEMEQ(2, "xx", v[0].atom)

	   && sexp_iterator_next(&v[0]) && v[0].type == SEXP_ATOM
	   && !v[0].display_length && !v[0].display
	   && v[0].atom_length == 3 && MEMEQ(3, "foo", v[0].atom)

	   && sexp_iterator_next(&v[0]) && v[0].type == SEXP_END);

    ASSERT(v[1].type == SEXP_END);

    ASSERT(sexp_iterator_first(&i, LDATA("((1:n))")));
    ASSERT(sexp_iterator_enter_list(&i)
	   && !sexp_iterator_assoc(&i, 2, keys, v));

    ASSERT(sexp_iterator_first(&i, LDATA("((1:n)(1:n3:foo))")));
    ASSERT(sexp_iterator_enter_list(&i)
	   && !sexp_iterator_assoc(&i, 2, keys, v));    
  }
}