PageRenderTime 465ms CodeModel.GetById 181ms app.highlight 9ms RepoModel.GetById 195ms app.codeStats 0ms

/src/test/text_buffer_test.c

http://ftk.googlecode.com/
C | 78 lines | 61 code | 17 blank | 0 comment | 33 complexity | fd2e565d14f170f9bd81335d3b3ada88 MD5 | raw file
 1#include "ftk.h"
 2#include "ftk_text_buffer.h"
 3
 4void english_test(void)
 5{
 6	int i = 0;
 7	FtkTextBuffer* thiz = ftk_text_buffer_create(6);
 8	assert(ftk_text_buffer_insert(thiz, 0, "b", -1) == RET_OK);
 9	assert(strcmp(thiz->buffer, "b") == 0);
10	assert(ftk_text_buffer_insert(thiz, 1, "c", -1) == RET_OK);
11	assert(strcmp(thiz->buffer, "bc") == 0);
12	assert(ftk_text_buffer_insert(thiz, 0, "a", -1) == RET_OK);
13	assert(strcmp(thiz->buffer, "abc") == 0);
14	assert(ftk_text_buffer_insert(thiz, 4, "a", -1) != RET_OK);
15	assert(strcmp(thiz->buffer, "abc") == 0);
16	assert(ftk_text_buffer_insert(thiz, 3, "defg", -1) == RET_OK);
17	assert(strcmp(thiz->buffer, "abcdefg") == 0);
18
19	assert(ftk_text_buffer_delete_chars(thiz, 0, 1) == RET_OK);
20	assert(strcmp(thiz->buffer, "bcdefg") == 0);
21	
22	assert(ftk_text_buffer_delete_chars(thiz, 0, 2) == RET_OK);
23	assert(strcmp(thiz->buffer, "defg") == 0);
24	
25	assert(ftk_text_buffer_delete_chars(thiz, 1, 2) == RET_OK);
26	assert(strcmp(thiz->buffer, "dg") == 0);
27	
28	assert(ftk_text_buffer_delete_chars(thiz, 1, -1) == RET_OK);
29	assert(strcmp(thiz->buffer, "g") == 0);
30	assert(ftk_text_buffer_delete_chars(thiz, 1, -1) == RET_OK);
31	assert(strcmp(thiz->buffer, "") == 0);
32
33	ftk_text_buffer_reset(thiz);
34	assert(thiz->length == 0);
35
36	for(i = 0; i < 1000; i++)
37	{
38		assert(strcmp(ftk_text_buffer_append_string(thiz, "123"), "123") == 0);
39		assert(thiz->length == (i+1) * 4);
40	}
41	ftk_text_buffer_destroy(thiz);
42
43	return;
44}
45
46void chinese_test(void)
47{
48	int offset = 0;
49	FtkTextBuffer* thiz = ftk_text_buffer_create(6);
50	assert(ftk_text_buffer_insert(thiz, 0, "??????", -1) == RET_OK);
51	offset  = ftk_text_buffer_chars_bytes(thiz, 0, 2);
52	assert(ftk_text_buffer_delete_chars(thiz, offset, -2) == RET_OK);
53	assert(strcmp(thiz->buffer, "????") == 0);
54	
55	assert(ftk_text_buffer_delete_chars(thiz, 0, 2) == RET_OK);
56	assert(strcmp(thiz->buffer, "??") == 0);
57	
58	assert(ftk_text_buffer_delete_chars(thiz, 0, 1) == RET_OK);
59	assert(strcmp(thiz->buffer, "?") == 0);
60	
61	offset  = ftk_text_buffer_chars_bytes(thiz, 0, 1);
62	assert(ftk_text_buffer_delete_chars(thiz, offset, -1) == RET_OK);
63	assert(strcmp(thiz->buffer, "") == 0);
64
65	ftk_text_buffer_destroy(thiz);
66
67	return;
68}
69
70int main(int argc, char* argv[])
71{
72	ftk_set_allocator(ftk_allocator_default_create());
73
74	english_test();
75	chinese_test();
76
77	return 0;
78}