summaryrefslogtreecommitdiff
path: root/src/node.test.c
blob: 26837c918a6f13bf58c614bb2a8f0d812c6ccd3e (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
101
102
103
104
105
106
107
108
109
110
111
#include <string.h>

#include <kalmia.h>
#include <ezxml.h>
#include "test/test.h"
#include "transform.h"
#include "node.h"


void empty();
void transforms();


void suite_node()
{
	lily_run_test(empty);
	lily_run_test(transforms);
}


void empty()
{
	char str[512];
	strncpy(
		str,
		"<node></node>",
		512
	);
	ezxml_t tag = ezxml_parse_str(str, strlen(str));

	kalmia_t k;
	k.node = NULL;
	k.n_nodes = 0;
	k.n_nodes_max = 0;

	ka_node_t *node = kai_parse_node(&k, tag);
	lily_assert_not_null(k.node);
	lily_assert_not_null(node);
	lily_assert_int_equal(k.n_nodes, 1);
	lily_assert_ptr_equal(k.node, node);

	ka_matrix_t identity;
	kai_identity(&identity);

	int i=0;
	for (i=0; i<16; i++) {
		lily_assert_float_equal(
			node->transform[i],
			identity[i],
			1e-3
		);
	}

	ezxml_free(tag);
	free(k.node);
}


void transforms()
{
	char str[512];
	strncpy(
		str,
		"<node>\n"
		"	<translate>10.0 2.0 5.0</translate>\n"
		"	<scale>5.0 1.0 2.0</scale>\n"
		"</node>",
		512
	);
	ezxml_t tag = ezxml_parse_str(str, strlen(str));

	kalmia_t k;
	k.node = NULL;
	k.n_nodes = 0;
	k.n_nodes_max = 0;

	ka_node_t *node = kai_parse_node(&k, tag);
	lily_assert_not_null(k.node);
	lily_assert_not_null(node);
	lily_assert_int_equal(k.n_nodes, 1);
	lily_assert_ptr_equal(k.node, node);

	ka_matrix_t translate = {
		1.0f, 0.0f, 0.0f, 10.0f,
		0.0f, 1.0f, 0.0f,  2.0f,
		0.0f, 0.0f, 1.0f,  5.0f,
		0.0f, 0.0f, 0.0f,  1.0f,
	};
	ka_matrix_t scale = {
		5.0f, 0.0f, 0.0f, 0.0f,
		0.0f, 1.0f, 0.0f, 0.0f,
		0.0f, 0.0f, 2.0f, 0.0f,
		0.0f, 0.0f, 0.0f, 1.0f,
	};

	ka_matrix_t expected;
	kai_multiply(&expected, translate, scale);


	int i=0;
	for (i=0; i<16; i++) {
		lily_assert_float_equal(
			node->transform[i],
			expected[i],
			1e-3
		);
	}

	ezxml_free(tag);
	free(k.node);
}