1
0
Fork 0
libfort/tests/wb_tests/test_string_buffer.c

370 lines
14 KiB
C
Raw Normal View History

2018-01-21 09:19:18 +01:00
#include "tests.h"
#include "string_buffer.h"
2019-05-24 20:25:33 +02:00
#include "wcwidth.h"
2018-05-06 13:11:03 +02:00
#include <wchar.h>
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
2018-03-31 12:33:37 +02:00
size_t strchr_count(const char *str, char ch);
size_t wstrchr_count(const wchar_t *str, wchar_t ch);
2018-03-05 19:08:14 +01:00
2018-03-31 12:33:37 +02:00
const char *str_n_substring_beg(const char *str, char ch_separator, size_t n);
const wchar_t *wstr_n_substring_beg(const wchar_t *str, wchar_t ch_separator, size_t n);
2018-03-05 19:08:14 +01:00
2018-03-31 12:33:37 +02:00
fort_status_t str_n_substring(const char *str, char ch_separator, size_t n, const char **begin, const char **end);
void wstr_n_substring(const wchar_t *str, wchar_t ch_separator, size_t n, const wchar_t **begin, const wchar_t **end);
2018-03-05 19:08:14 +01:00
2018-11-25 15:25:48 +01:00
//size_t buffer_text_width(string_buffer_t *buffer);
2018-01-21 09:19:18 +01:00
void test_strchr_count(void);
void test_str_n_substring(void);
void test_buffer_text_width(void);
void test_buffer_text_height(void);
2019-05-24 20:25:33 +02:00
#if defined(FT_HAVE_WCHAR)
void test_wchar_basics(void);
#endif
2018-01-21 09:19:18 +01:00
2018-03-19 21:07:18 +01:00
void test_string_buffer(void)
2018-01-21 09:19:18 +01:00
{
test_strchr_count();
test_str_n_substring();
test_buffer_text_width();
test_buffer_text_height();
2019-05-24 20:25:33 +02:00
#if defined(FT_HAVE_WCHAR)
test_wchar_basics();
#endif
2018-01-21 09:19:18 +01:00
}
void test_strchr_count(void)
{
assert_true(strchr_count(NULL, '\n') == 0);
assert_true(strchr_count("", '\n') == 0);
assert_true(strchr_count("asbd", '\n') == 0);
assert_true(strchr_count("asbd\n", '\n') == 1);
assert_true(strchr_count("\nasbd", '\n') == 1);
assert_true(strchr_count("a\nsbd", '\n') == 1);
assert_true(strchr_count("\n12\n123", '\n') == 2);
assert_true(strchr_count("\n12\n123\n", '\n') == 3);
assert_true(strchr_count("\n\n\n", '\n') == 3);
assert_true(strchr_count("\n123123\n123123\n\n\n123", '\n') == 5);
assert_true(strchr_count("1a23123a123123aaa123", 'a') == 5);
2018-03-05 19:08:14 +01:00
assert_true(wstrchr_count(NULL, L'\n') == 0);
assert_true(wstrchr_count(L"", L'\n') == 0);
assert_true(wstrchr_count(L"asbd", L'\n') == 0);
assert_true(wstrchr_count(L"asbd\n", L'\n') == 1);
assert_true(wstrchr_count(L"\nasbd", L'\n') == 1);
assert_true(wstrchr_count(L"a\nsbd", L'\n') == 1);
assert_true(wstrchr_count(L"\n12\n123", L'\n') == 2);
assert_true(wstrchr_count(L"\n12\n123\n", L'\n') == 3);
assert_true(wstrchr_count(L"\n\n\n", '\n') == 3);
assert_true(wstrchr_count(L"\n123123\n123123\n\n\n123", L'\n') == 5);
assert_true(wstrchr_count(L"1\xffffy23123\xffffy123123\xffffy\xffffy\xffffy123", L'\xffff') == 5);
2018-01-21 09:19:18 +01:00
}
void test_str_n_substring(void)
{
const char *empty_str = "";
assert_true(str_n_substring_beg(empty_str, '\n', 0) == empty_str);
assert_true(str_n_substring_beg(empty_str, '\n', 1) == NULL);
assert_true(str_n_substring_beg(empty_str, '\n', 2) == NULL);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
const wchar_t *empty_wstr = L"";
assert_true(wstr_n_substring_beg(empty_wstr, L'\n', 0) == empty_wstr);
assert_true(wstr_n_substring_beg(empty_wstr, L'\n', 1) == NULL);
assert_true(wstr_n_substring_beg(empty_wstr, L'\n', 2) == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
const char *str = "123\n5678\n9";
assert_true(str_n_substring_beg(NULL, '\n', 0) == NULL);
assert_true(str_n_substring_beg(str, '\n', 0) == str);
assert_true(str_n_substring_beg(str, '1', 0) == str);
assert_true(str_n_substring_beg(str, '\n', 1) == str + 4);
assert_true(str_n_substring_beg(str, '\n', 2) == str + 9);
assert_true(str_n_substring_beg(str, '\n', 3) == NULL);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
const wchar_t *wstr = L"123\n5678\n9";
assert_true(wstr_n_substring_beg(NULL, L'\n', 0) == NULL);
assert_true(wstr_n_substring_beg(wstr, L'\n', 0) == wstr);
assert_true(wstr_n_substring_beg(wstr, L'1', 0) == wstr);
assert_true(wstr_n_substring_beg(wstr, L'\n', 1) == wstr + 4);
assert_true(wstr_n_substring_beg(wstr, L'\n', 2) == wstr + 9);
assert_true(wstr_n_substring_beg(wstr, L'\n', 3) == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
const char *str2 = "\n123\n56\n\n9\n";
assert_true(str_n_substring_beg(str2, '\n', 0) == str2);
assert_true(str_n_substring_beg(str2, '\n', 1) == str2 + 1);
assert_true(str_n_substring_beg(str2, '\n', 2) == str2 + 5);
assert_true(str_n_substring_beg(str2, '\n', 3) == str2 + 8);
assert_true(str_n_substring_beg(str2, '\n', 4) == str2 + 9);
assert_true(str_n_substring_beg(str2, '\n', 5) == str2 + 11);
assert_true(str_n_substring_beg(str2, '\n', 6) == NULL);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
const wchar_t *wstr2 = L"\xff0fy23\xff0fy6\xff0f\xff0fy\xff0f";
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 0) == wstr2);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 1) == wstr2 + 1);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 2) == wstr2 + 5);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 3) == wstr2 + 8);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 4) == wstr2 + 9);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 5) == wstr2 + 11);
assert_true(wstr_n_substring_beg(wstr2, L'\xff0f', 6) == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
const char *beg = NULL;
const char *end = NULL;
str_n_substring(empty_str, '\n', 0, &beg, &end);
assert_true(beg == empty_str && end == empty_str + strlen(empty_str));
str_n_substring(empty_str, '\n', 1, &beg, &end);
assert_true(beg == NULL && end == NULL);
str_n_substring(empty_str, '\n', 2, &beg, &end);
assert_true(beg == NULL && end == NULL);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
const wchar_t *wbeg = NULL;
const wchar_t *wend = NULL;
wstr_n_substring(empty_wstr, L'\n', 0, &wbeg, &wend);
assert_true(wbeg == empty_wstr && wend == empty_wstr + wcslen(empty_wstr));
wstr_n_substring(empty_wstr, L'\n', 1, &wbeg, &wend);
assert_true(wbeg == NULL && wend == NULL);
wstr_n_substring(empty_wstr, L'\n', 2, &wbeg, &wend);
assert_true(wbeg == NULL && wend == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
str_n_substring(NULL, '\n', 0, &beg, &end);
assert_true(beg == NULL && end == NULL);
str_n_substring(str, '\n', 0, &beg, &end);
assert_true(beg == str && end == str + 3);
str_n_substring(str, '2', 0, &beg, &end);
assert_true(beg == str && end == str + 1);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
wstr_n_substring(NULL, L'\n', 0, &wbeg, &wend);
assert_true(wbeg == NULL && wend == NULL);
wstr_n_substring(wstr, L'\n', 0, &wbeg, &wend);
assert_true(wbeg == wstr && wend == wstr + 3);
wstr_n_substring(wstr, L'2', 0, &wbeg, &wend);
assert_true(wbeg == wstr && wend == wstr + 1);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
2018-01-21 09:19:18 +01:00
str_n_substring(str, '\n', 1, &beg, &end);
2018-03-31 12:33:37 +02:00
assert_true(beg == str + 4 && end == str + 8);
2018-01-21 09:19:18 +01:00
str_n_substring(str, '\n', 2, &beg, &end);
assert_true(beg == str + 9 && end == str + strlen(str));
str_n_substring(str, '\n', 3, &beg, &end);
assert_true(beg == NULL && end == NULL);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
wstr_n_substring(wstr, L'\n', 1, &wbeg, &wend);
2018-03-31 12:33:37 +02:00
assert_true(wbeg == wstr + 4 && wend == wstr + 8);
2018-03-05 19:08:14 +01:00
wstr_n_substring(wstr, L'\n', 2, &wbeg, &wend);
assert_true(wbeg == wstr + 9 && wend == wstr + wcslen(wstr));
wstr_n_substring(wstr, L'\n', 3, &wbeg, &wend);
assert_true(wbeg == NULL && wend == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-01-21 09:19:18 +01:00
str_n_substring(str2, '\n', 0, &beg, &end);
assert_true(beg == str2 && end == str2);
str_n_substring(str2, '\n', 1, &beg, &end);
assert_true(beg == str2 + 1 && end == str2 + 4);
str_n_substring(str2, '\n', 2, &beg, &end);
assert_true(beg == str2 + 5 && end == str2 + 7);
str_n_substring(str2, '\n', 3, &beg, &end);
assert_true(beg == str2 + 8 && end == str2 + 8);
str_n_substring(str2, '\n', 4, &beg, &end);
assert_true(beg == str2 + 9 && end == str2 + 10);
str_n_substring(str2, '\n', 5, &beg, &end);
assert_true(beg == str2 + 11 && end == str2 + 11);
str_n_substring(str2, '\n', 6, &beg, &end);
assert_true(beg == NULL && end == NULL);
2018-03-05 19:08:14 +01:00
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
wstr_n_substring(wstr2, L'\xff0f', 0, &wbeg, &wend);
assert_true(wbeg == wstr2 && wend == wstr2);
wstr_n_substring(wstr2, L'\xff0f', 1, &wbeg, &wend);
assert_true(wbeg == wstr2 + 1 && wend == wstr2 + 4);
wstr_n_substring(wstr2, L'\xff0f', 2, &wbeg, &wend);
assert_true(wbeg == wstr2 + 5 && wend == wstr2 + 7);
wstr_n_substring(wstr2, L'\xff0f', 3, &wbeg, &wend);
assert_true(wbeg == wstr2 + 8 && wend == wstr2 + 8);
wstr_n_substring(wstr2, L'\xff0f', 4, &wbeg, &wend);
assert_true(wbeg == wstr2 + 9 && wend == wstr2 + 10);
wstr_n_substring(wstr2, L'\xff0f', 5, &wbeg, &wend);
assert_true(wbeg == wstr2 + 11 && wend == wstr2 + 11);
wstr_n_substring(wstr2, L'\xff0f', 6, &wbeg, &wend);
assert_true(wbeg == NULL && wend == NULL);
2018-11-16 19:27:25 +01:00
#endif
2018-01-21 09:19:18 +01:00
}
void test_buffer_text_width(void)
{
2018-03-05 19:08:14 +01:00
string_buffer_t *buffer = create_string_buffer(200, CharBuf);
buffer->type = CharBuf;
char *old_value = buffer->str.cstr;
2018-03-05 19:08:14 +01:00
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 0);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n\n\n\n";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 0);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"12345";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 5);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"12345\n1234567";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"12345\n1234567\n";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"12345\n1234567\n123";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
buffer->type = WCharBuf;
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 0);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n\n\n\n";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 0);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"12345";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 5);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"12345\n1234567";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"12345\n1234567\n";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"12345\n1234567\n123";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_width(buffer) == 7);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
buffer->type = CharBuf;
buffer->str.cstr = old_value;
2018-01-21 09:19:18 +01:00
destroy_string_buffer(buffer);
}
void test_buffer_text_height(void)
{
2018-03-05 19:08:14 +01:00
string_buffer_t *buffer = create_string_buffer(200, CharBuf);
buffer->type = CharBuf;
char *old_value = buffer->str.cstr;
2018-01-21 09:19:18 +01:00
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 0);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 2);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n\n";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 3);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n\n\n\n";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 5);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"12345";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 1);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n12345";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 2);
2018-03-31 12:33:37 +02:00
buffer->str.cstr = (char *)"\n12345\n\n2";
2018-01-21 09:19:18 +01:00
assert_true(buffer_text_height(buffer) == 4);
2018-11-16 19:27:25 +01:00
#if defined(FT_HAVE_WCHAR)
2018-03-05 19:08:14 +01:00
buffer->type = WCharBuf;
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 0);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 2);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n\n";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 3);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n\n\n\n";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 5);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\xff0fy2345\xff0f";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 1);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n12345";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 2);
2018-03-18 10:17:33 +01:00
buffer->str.wstr = (wchar_t *)L"\n12345\n\n2";
2018-03-05 19:08:14 +01:00
assert_true(buffer_text_height(buffer) == 4);
2018-11-16 19:27:25 +01:00
#endif
2018-03-05 19:08:14 +01:00
buffer->type = CharBuf;
buffer->str.cstr = old_value;
2018-01-21 09:19:18 +01:00
destroy_string_buffer(buffer);
}
2019-05-24 20:25:33 +02:00
#if defined(FT_HAVE_WCHAR)
void test_wchar_basics(void)
{
#if !defined(FT_MICROSOFT_COMPILER)
assert_true(mk_wcswidth(L"", 0) == 0);
assert_true(mk_wcswidth(L"1", 0) == 0);
assert_true(mk_wcswidth(L"1", 1) == 1);
assert_true(mk_wcswidth(L"λ", 1) == 1); // Greek
assert_true(mk_wcswidth(L"", 1) == 1); // Hindi
assert_true(mk_wcswidth(L"ź", 1) == 1); // Polish
assert_true(mk_wcswidth(L"ü", 1) == 1); // Portuguese
assert_true(mk_wcswidth(L"ц", 1) == 1); // Russian
assert_true(mk_wcswidth(L"ñ", 1) == 1); // Spanish
assert_true(mk_wcswidth(L"ğ", 1) == 1); // Turkish
assert_true(mk_wcswidth(L"ФЦУЙъхЭЯЧьЮЪЁ", 13) == 13);
assert_true(mk_wcswidth(L"ФЦУЙъхЭЯЧьЮЪЁ", 14) == 13);
assert_true(mk_wcswidth(L"ФЦУЙъхЭЯЧьЮЪЁ", 10) == 10);
/* panagrams from http://clagnut.com/blog/2380/ */
const wchar_t *str = NULL;
/* 10 20 30 40 50 60 70 80 90 100 110 */
str = L"Numbers 01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
assert_true(mk_wcswidth(str, 500) == 110);
str = L"German Falsches Üben von Xylophonmusik quält jeden größeren Zwerg";
assert_true(mk_wcswidth(str, 500) == 68);
str = L"Greek Ταχίστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός Takhístè";
assert_true(mk_wcswidth(str, 500) == 80);
str = L"Irish Dḟuascail Íosa Úrṁac na hÓiġe Beannaiṫe pór Éaḃa agus Áḋaiṁ";
assert_true(mk_wcswidth(str, 500) == 70);
str = L"Polish Pójdźże, kiń tę chmurność w głąb flaszy";
assert_true(mk_wcswidth(str, 500) == 49);
str = L"Portuguese Luís argüia à Júlia que «brações, fé, chá, óxido, pôr, zângão» eram palavras do português";
assert_true(mk_wcswidth(str, 500) == 100);
str = L"Russian Съешь же ещё этих мягких французских булок, да выпей чаю";
assert_true(mk_wcswidth(str, 500) == 66);
str = L"Spanish Benjamín pidió una bebida de kiwi y fresa; Noé, sin vergüenza, la más exquisita champaña del menú";
assert_true(mk_wcswidth(str, 500) == 107);
str = L"Turkish Vakfın çoğu bu huysuz genci plajda görmüştü";
assert_true(mk_wcswidth(str, 500) == 53);
#endif
}
#endif