#include "../note.h"
+#include <regex.h>
CX_TEST(test_text_search_strcasestr) {
CX_TEST_DO {
// test from pos 0
int begin = -1;
int end = -1;
- int ret = text_search(str, cx_str("123"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ int ret = text_search(str, cx_str("123"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 0);
CX_TEST_ASSERT(end == 3);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("4567"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("4567"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 4);
CX_TEST_ASSERT(end == 8);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("edfghj"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("edfghj"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 18);
CX_TEST_ASSERT(end == 24);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("abc"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("abc"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 13);
CX_TEST_ASSERT(end == 16);
// test from other positions
begin = -1;
end = -1;
- ret = text_search(str, cx_str("4567"), 4, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("4567"), 4, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 4);
CX_TEST_ASSERT(end == 8);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("abc"), 14, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("abc"), 14, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 33);
CX_TEST_ASSERT(end == 36);
- ret = text_search(str, cx_str("4567"), 20, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("4567"), 20, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(!ret);
- ret = text_search(str, cx_str("edfghj"), 19, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("edfghj"), 19, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(!ret);
// case sensitive test
- ret = text_search(str, cx_str("ABCD"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("ABCD"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(!ret);
- ret = text_search(str, cx_str("Xyz"), 0, FALSE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("Xyz"), 0, FALSE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(!ret);
// backwards search
begin = -1;
end = -1;
- ret = text_search(str, cx_str("abc"), 36, TRUE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("abc"), 36, TRUE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 33);
CX_TEST_ASSERT(end == 36);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("edf"), 25, TRUE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("edf"), 25, TRUE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 18);
CX_TEST_ASSERT(end == 21);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("abc"), 25, TRUE, TRUE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("abc"), 25, TRUE, TRUE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 13);
CX_TEST_ASSERT(end == 16);
// test from pos 0
int begin = -1;
int end = -1;
- int ret = text_search(str, cx_str("abc"), 0, FALSE, FALSE, FALSE, &begin, &end);
+ int ret = text_search(str, cx_str("abc"), 0, FALSE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 0);
CX_TEST_ASSERT(end == 3);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("def"), 0, FALSE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("def"), 0, FALSE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 4);
CX_TEST_ASSERT(end == 7);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("DEF"), 0, FALSE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("DEF"), 0, FALSE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 4);
CX_TEST_ASSERT(end == 7);
// test other pos
begin = -1;
end = -1;
- ret = text_search(str, cx_str("DEF"), 4, FALSE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("DEF"), 4, FALSE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 4);
CX_TEST_ASSERT(end == 7);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("ABC"), 8, FALSE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("ABC"), 8, FALSE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 16);
CX_TEST_ASSERT(end == 19);
// backwards
begin = -1;
end = -1;
- ret = text_search(str, cx_str("aBc"), 19, TRUE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("aBc"), 19, TRUE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 16);
CX_TEST_ASSERT(end == 19);
begin = -1;
end = -1;
- ret = text_search(str, cx_str("xyz"), 19, TRUE, FALSE, FALSE, &begin, &end);
+ ret = text_search(str, cx_str("xyz"), 19, TRUE, FALSE, NULL, &begin, &end);
CX_TEST_ASSERT(ret);
CX_TEST_ASSERT(begin == 12);
CX_TEST_ASSERT(end == 15);
}
}
+
+CX_TEST(test_text_search_regex) {
+ CX_TEST_DO {
+ cxstring str = cx_str("hello world test string text end");
+
+ regex_t regex;
+ CX_TEST_ASSERT(!regcomp(®ex, "te[^ ]* ", REG_EXTENDED));
+
+ int begin, end, ret;
+
+ // from pos 0
+ begin = -1;
+ end = -1;
+ ret = text_search(str, cx_str(""), 0, FALSE, FALSE, ®ex, &begin, &end);
+ CX_TEST_ASSERT(ret);
+ CX_TEST_ASSERT(begin == 12);
+ CX_TEST_ASSERT(end == 17);
+
+ // from pos > 0
+ begin = -1;
+ end = -1;
+ ret = text_search(str, cx_str(""), 4, FALSE, FALSE, ®ex, &begin, &end);
+ CX_TEST_ASSERT(ret);
+ CX_TEST_ASSERT(begin == 12);
+ CX_TEST_ASSERT(end == 17);
+
+ // negative
+ begin = -1;
+ end = -1;
+ ret = text_search(str, cx_str(""), 26, FALSE, FALSE, ®ex, &begin, &end);
+ CX_TEST_ASSERT(!ret);
+
+ // backwards
+ begin = -1;
+ end = -1;
+ ret = text_search(str, cx_str(""), 20, TRUE, FALSE, ®ex, &begin, &end);
+ CX_TEST_ASSERT(ret);
+ CX_TEST_ASSERT(begin == 12);
+ CX_TEST_ASSERT(end == 17);
+ }
+
+}