forked from lc-soft/LCUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_object.c
149 lines (127 loc) · 4.72 KB
/
test_object.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <wchar.h>
#include <math.h>
#include <string.h>
#include <LCUI_Build.h>
#include <LCUI/util/logger.h>
#include <LCUI/util/object.h>
#include "test.h"
#include "libtest.h"
static void on_object_change(LCUI_Object object, void *data)
{
int *count = data;
*count += 1;
}
static void test_string_object(void)
{
int object_changes = 0;
LCUI_Object tmp;
LCUI_Object str1 = String_New("hello");
LCUI_Object str2 = String_New("world");
LCUI_ObjectRec str3;
LCUI_ObjectWatcher watcher;
watcher = Object_Watch(str1, on_object_change, &object_changes);
it_b("check Object_Watch() is valid", watcher != NULL, TRUE);
String_Init(&str3, "helloworld");
tmp = Object_Operate(str1, "+", str2);
it_i("check string object concatenation", Object_Compare(&str3, tmp),
0);
it_b("check string object append stores result in correct object",
Object_Operate(str1, "+=", str2) == str1, TRUE);
it_i("check string object append has correct result",
Object_Compare(str1, &str3), 0);
it_s("check raw string from string object", str1->value.string,
"helloworld");
it_i("check object change notification", object_changes, 1);
ObjectWatcher_Delete(watcher);
Object_Delete(tmp);
Object_Delete(str1);
Object_Delete(str2);
Object_Destroy(&str3);
}
static void test_wstring_object(void)
{
LCUI_Object tmp;
LCUI_Object str1 = WString_New(L"测试");
LCUI_Object str2 = WString_New(L"文本");
LCUI_ObjectRec str3;
LCUI_Object str4;
WString_Init(&str3, L"测试文本");
tmp = Object_Operate(str1, "+", str2);
it_i("check wstring object concatenation", Object_Compare(&str3, tmp),
0);
it_b("check wstring object append stores result in correct object",
Object_Operate(str1, "+=", str2) == str1, TRUE);
it_i("check wstring object append has correct result",
Object_Compare(str1, &str3), 0);
it_b("check raw wstring from string object",
wcscmp(str1->value.wstring, L"测试文本") == 0, TRUE);
WString_SetValue(&str3, L"1000");
str4 = Object_ToString(&str3);
it_s("check convert wstring object to raw string", str4->value.string,
"1000");
Object_Delete(tmp);
Object_Delete(str1);
Object_Delete(str2);
Object_Delete(str4);
Object_Destroy(&str3);
}
static void test_number_object(void)
{
int object_changes = 0;
LCUI_Object tmp;
LCUI_Object str;
LCUI_ObjectRec num1;
LCUI_ObjectRec num2;
LCUI_ObjectWatcher watcher;
Number_Init(&num1, 1.25);
Number_Init(&num2, 4.0);
watcher = Object_Watch(&num1, on_object_change, &object_changes);
it_b("check Object_Watch() is valid", watcher != NULL, TRUE);
tmp = Object_Operate(&num1, "*", &num2);
it_b("check number object *= stores result in left object",
Object_Operate(&num1, "*=", &num2) == &num1, TRUE);
it_i("check result of number object *=", Object_Compare(&num1, tmp), 0);
it_b("check raw value of number object", tmp->value.number == 5.0,
TRUE);
it_i("check object change notifications", object_changes, 1);
Object_Delete(tmp);
tmp = Object_Operate(&num1, "/", &num2);
it_b("check number object /= stores result in left object",
Object_Operate(&num1, "/=", &num2) == &num1, TRUE);
it_i("check result of number object /=", Object_Compare(&num1, tmp), 0);
it_b("check raw value of number", tmp->value.number == 1.25, TRUE);
it_i("check number object change notifications", object_changes, 2);
Object_Delete(tmp);
Number_SetValue(&num1, 4.5);
Number_SetValue(&num2, 5.5);
it_i("check numnber object change notifications", object_changes, 3);
tmp = Object_Operate(&num1, "+", &num2);
it_b("check number object += stores result in left object",
Object_Operate(&num1, "+=", &num2) == &num1, TRUE);
it_i("check result of number object +=", Object_Compare(&num1, tmp), 0);
it_b("check raw value of number", tmp->value.number == 10.0, TRUE);
it_i("check number of object change notifications", object_changes, 4);
Object_Delete(tmp);
Number_SetValue(&num1, 12.5);
Number_SetValue(&num2, 7.5);
it_i("check number of object change notifications", object_changes, 5);
tmp = Object_Operate(&num1, "-", &num2);
it_b("check number object -= stores result in left object",
Object_Operate(&num1, "-=", &num2) == &num1, TRUE);
it_i("check result of number object -=", Object_Compare(&num1, tmp), 0);
it_b("check raw value of number", tmp->value.number == 5.0, TRUE);
it_i("check number of object change notifications", object_changes, 6);
Object_Delete(tmp);
str = Object_ToString(&num1);
it_s("check result of converting number object to string",
str->value.string, "5");
ObjectWatcher_Delete(watcher);
Object_Destroy(&num1);
Object_Delete(str);
}
void test_object(void)
{
describe("test string object", test_string_object);
describe("test wstring object", test_wstring_object);
describe("test number object", test_number_object);
}