| 1 | #include "cppjieba/Unicode.hpp" |
| 2 | #include "cppjieba/Utils.hpp" |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | using namespace cppjieba; |
| 6 | using namespace std; |
| 7 | |
| 8 | TEST(UnicodeTest, Test1) { |
| 9 | string s = "你好世界"; |
| 10 | RuneStrArray runes; |
| 11 | ASSERT_TRUE(DecodeUTF8RunesInString(s, runes)); |
| 12 | string actual; |
| 13 | string expected = "[{\"rune\": \"20320\", \"offset\": 0, \"len\": 3}, {\"rune\": \"22909\", \"offset\": 3, \"len\": 3}, {\"rune\": \"19990\", \"offset\": 6, \"len\": 3}, {\"rune\": \"30028\", \"offset\": 9, \"len\": 3}]"; |
| 14 | actual << runes; |
| 15 | ASSERT_EQ(expected, actual); |
| 16 | } |
| 17 | |
| 18 | TEST(UnicodeTest, Illegal) { |
| 19 | string s = "123\x80"; |
| 20 | RuneStrArray runes; |
| 21 | ASSERT_FALSE(DecodeUTF8RunesInString(s, runes)); |
| 22 | string actual; |
| 23 | string expected = "[]"; |
| 24 | actual << runes; |
| 25 | ASSERT_EQ(expected, actual); |
| 26 | } |
| 27 | |
| 28 | TEST(UnicodeTest, Rand) { |
| 29 | const size_t ITERATION = 1024; |
| 30 | const size_t MAX_LEN = 256; |
| 31 | string s; |
| 32 | srand(time(NULL)); |
| 33 | |
| 34 | for (size_t i = 0; i < ITERATION; i++) { |
| 35 | size_t len = rand() % MAX_LEN; |
| 36 | s.resize(len); |
| 37 | for (size_t j = 0; j < len; j++) { |
| 38 | s[rand() % len] = rand(); |
| 39 | } |
| 40 | RuneStrArray runes; |
| 41 | DecodeUTF8RunesInString(s, runes); |
| 42 | } |
| 43 | } |