example: Fix a build warning.
[platform/upstream/cmocka.git] / example / key_value_test.c
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include <setjmp.h>
19 #include <string.h>
20 #include <cmocka.h>
21
22 #include "key_value.h"
23
24 static KeyValue key_values[] = {
25     { 10, "this" },
26     { 52, "test" },
27     { 20, "a" },
28     { 13, "is" },
29 };
30
31 static void create_key_values(void **state) {
32     KeyValue * const items = (KeyValue*)test_malloc(sizeof(key_values));
33     memcpy(items, key_values, sizeof(key_values));
34     *state = (void*)items;
35     set_key_values(items, sizeof(key_values) / sizeof(key_values[0]));
36 }
37
38 static void destroy_key_values(void **state) {
39     test_free(*state);
40     set_key_values(NULL, 0);
41 }
42
43 static void test_find_item_by_value(void **state) {
44     unsigned int i;
45
46     (void) state; /* unused */
47
48     for (i = 0; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
49         KeyValue * const found  = find_item_by_value(key_values[i].value);
50         assert_true(found != NULL);
51         assert_int_equal(found->key, key_values[i].key);
52         assert_string_equal(found->value, key_values[i].value);
53     }
54 }
55
56 static void test_sort_items_by_key(void **state) {
57     unsigned int i;
58     KeyValue * const kv = *state;
59     sort_items_by_key();
60     for (i = 1; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
61         assert_true(kv[i - 1].key < kv[i].key);
62     }
63 }
64
65 int main(void) {
66     const UnitTest tests[] = {
67         unit_test_setup_teardown(test_find_item_by_value, create_key_values,
68                                  destroy_key_values),
69         unit_test_setup_teardown(test_sort_items_by_key, create_key_values,
70                                  destroy_key_values),
71     };
72     return run_tests(tests);
73 }