Add CLion style.
[SquirrelJME.git] / nanocoat / tests / testTraversePut.c
blobe5f74d134792272a41ac0e486eb2388320c6f756
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "test.h"
13 #include "proto.h"
14 #include "mock.h"
15 #include "unit.h"
17 #include "sjme/traverse.h"
18 #include "testTraverse.h"
20 /**
21 * Tests putting values into the iteration.
23 * @since 2024/09/01
25 SJME_TEST_DECLARE(testTraversePut)
27 sjme_traverse_test_data traverse;
28 test_data value;
30 /* Setup traverse. */
31 traverse = NULL;
32 if (sjme_error_is(test->error = sjme_traverse_new(test->pool,
33 &traverse, MAX_ELEMENTS, test_data, 0)) || traverse == NULL)
34 return sjme_unit_fail(test, "Could not create traverse?");
36 /* Store first value. */
37 memset(&value, 0, sizeof(value));
38 value.a = 1;
39 value.b = 10;
40 if (sjme_error_is(test->error = sjme_traverse_put(traverse,
41 &value, 0, 1, test_data, 0)))
42 return sjme_unit_fail(test, "Could not put in first value?");
44 /* There should be a root and zero facing leaf. */
45 sjme_unit_notEqualP(test, NULL, traverse->root,
46 "Root node missing?");
47 sjme_unit_notEqualP(test,
48 (void*)SJME_TRAVERSE_LEAF_KEY, (void*)traverse->root->leaf.key,
49 "Root node not a node?");
50 sjme_unit_notEqualP(test, NULL, traverse->root->node.zero,
51 "There is no zero node?");
52 sjme_unit_equalP(test, NULL, traverse->root->node.one,
53 "There is a one node?");
54 sjme_unit_equalP(test,
55 (void*)SJME_TRAVERSE_LEAF_KEY,
56 (void*)traverse->root->node.zero->leaf.key,
57 "Zero facing is not a leaf?");
58 sjme_unit_equalI(test,
59 0, memcmp(&value,
60 &traverse->root->node.zero->leaf.value[0],
61 sizeof(test_data)),
62 "Incorrect value?");
64 /* Store second value. */
65 memset(&value, 0, sizeof(value));
66 value.a = 2;
67 value.b = 20;
68 if (sjme_error_is(test->error = sjme_traverse_put(traverse,
69 &value, 1, 1, test_data, 0)))
70 return sjme_unit_fail(test, "Could not put in second value?");
72 /* There should be a root and one facing leaf. */
73 sjme_unit_notEqualP(test, NULL, traverse->root,
74 "Root node missing?");
75 sjme_unit_notEqualP(test,
76 (void*)SJME_TRAVERSE_LEAF_KEY, (void*)traverse->root->leaf.key,
77 "Root node not a node?");
78 sjme_unit_notEqualP(test, NULL, traverse->root->node.zero,
79 "There is no zero node?");
80 sjme_unit_notEqualP(test, NULL, traverse->root->node.one,
81 "There is no one node?");
82 sjme_unit_equalP(test,
83 (void*)SJME_TRAVERSE_LEAF_KEY,
84 (void*)traverse->root->node.one->leaf.key,
85 "One facing is not a leaf?");
86 sjme_unit_equalI(test,
87 0, memcmp(&value,
88 &traverse->root->node.one->leaf.value[0],
89 sizeof(test_data)),
90 "Incorrect value?");
92 /* Destroy traverse. */
93 if (sjme_error_is(test->error = sjme_traverse_destroy(
94 SJME_AS_TRAVERSE(traverse))))
95 return sjme_unit_fail(test, "Could not destroy traverse?");
97 /* Success! */
98 return SJME_TEST_RESULT_PASS;