Blame view

RIOT/tests/unittests/tests-ubjson/test-ubjson-empty-object.c 4.15 KB
a752c7ab   elopes   add first test an...
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  #include "tests-ubjson.h"
  
  typedef enum {
      BEFORE_ARRAY_1 = __LINE__,
      IN_ARRAY_1 = __LINE__,
  
      BEFORE_ARRAY_2 = __LINE__,
      IN_ARRAY_2 = __LINE__,
  
      BEFORE_END = __LINE__,
  } test_ubjson_empty_object_state_t;
  
  typedef struct {
      ubjson_cookie_t cookie;
      test_ubjson_empty_object_state_t state;
  } test_ubjson_empty_object_receiver_cookie_t;
  
  static void test_ubjson_empty_object_receiver_callback_sub(
          ubjson_cookie_t *restrict cookie,
          ubjson_type_t type1, ssize_t content1,
          ubjson_type_t type2, ssize_t content2,
          ubjson_read_callback_result_t *result)
  {
      *result = UBJSON_ABORTED;
  
      (void) content1, (void) type2, (void) content2;
  
      test_ubjson_empty_object_receiver_cookie_t *state;
      state = container_of(cookie, test_ubjson_empty_object_receiver_cookie_t, cookie);
  
      switch (state->state) {
          case BEFORE_ARRAY_1:
              TEST_ASSERT_EQUAL_INT(UBJSON_ENTER_OBJECT, type1);
              state->state = IN_ARRAY_1;
              TEST_ASSERT_EQUAL_INT(UBJSON_OKAY, ubjson_read_object(cookie));
              state->state = BEFORE_ARRAY_2;
              break;
  
          case IN_ARRAY_1:
              TEST_FAIL("The empty object was not empty (1)");
              break;
  
          case BEFORE_ARRAY_2:
              TEST_ASSERT_EQUAL_INT(UBJSON_ENTER_OBJECT, type1);
              state->state = IN_ARRAY_2;
              TEST_ASSERT_EQUAL_INT(UBJSON_OKAY, ubjson_read_object(cookie));
              state->state = BEFORE_END;
              break;
  
          case IN_ARRAY_2:
              TEST_FAIL("The empty object was not empty (2)");
              break;
  
          case BEFORE_END:
              TEST_FAIL("Content after the end");
              break;
  
          default:
              TEST_FAIL("The cookie was corrupted");
              break;
      }
  
      *result = UBJSON_OKAY;
  }
  
  static ubjson_read_callback_result_t test_ubjson_empty_object_receiver_callback(
          ubjson_cookie_t *restrict cookie,
          ubjson_type_t type1, ssize_t content1,
          ubjson_type_t type2, ssize_t content2)
  {
      ubjson_read_callback_result_t result;
      test_ubjson_empty_object_receiver_callback_sub(cookie, type1, content1, type2, content2, &result);
      return result;
  }
  
  #undef EMBUNIT_ERROR_RETURN
  #define EMBUNIT_ERROR_RETURN
  
  static void test_ubjson_empty_object_receiver(void)
  {
      test_ubjson_empty_object_receiver_cookie_t state;
      state.state = BEFORE_ARRAY_1;
  
      TEST_ASSERT_EQUAL_INT(UBJSON_OKAY,
                            ubjson_read(&state.cookie, test_ubjson_read_fun,
                                        test_ubjson_empty_object_receiver_callback));
      TEST_ASSERT_EQUAL_INT(BEFORE_ARRAY_2, state.state);
  
      TEST_ASSERT_EQUAL_INT(UBJSON_OKAY,
                            ubjson_read(&state.cookie, test_ubjson_read_fun,
                                        test_ubjson_empty_object_receiver_callback));
      TEST_ASSERT_EQUAL_INT(BEFORE_END, state.state);
  }
  
  #undef EMBUNIT_ERROR_RETURN
  #define EMBUNIT_ERROR_RETURN
  
  static void test_ubjson_empty_object_sender(void)
  {
      ubjson_cookie_t cookie;
      ubjson_write_init(&cookie, test_ubjson_write_fun);
  
      TEST_ASSERT(ubjson_open_object(&cookie) > 0);
      TEST_ASSERT(ubjson_close_object(&cookie) > 0);
      TEST_ASSERT(ubjson_open_object_len(&cookie, 0) > 0);
  }
  
  void test_ubjson_empty_object(void)
  {
      test_ubjson_test(test_ubjson_empty_object_sender,
                       test_ubjson_empty_object_receiver);
  }