| 12 | namespace enc { |
| 13 | template <typename Encoder, typename DfTest> |
| 14 | void TestOrdinalEncoderStrs() { |
| 15 | Encoder encoder; |
| 16 | auto sol = std::vector<std::int32_t>{0, 3, 1}; |
| 17 | |
| 18 | { |
| 19 | auto df = DfTest::Make(DfTest::MakeStrs("c", "b", "d", "a")); |
| 20 | auto orig_dict = df.View(); |
| 21 | ASSERT_EQ(orig_dict.Size(), 1); |
| 22 | |
| 23 | auto new_df = DfTest::Make(DfTest::MakeStrs("c", "a", "b")); |
| 24 | auto new_dict = new_df.View(); |
| 25 | |
| 26 | encoder.Recode(orig_dict, new_dict, new_df.MappingView()); |
| 27 | ASSERT_EQ(new_df.Mapping().size(), 3); |
| 28 | |
| 29 | ASSERT_EQ(new_df.Mapping(), sol); |
| 30 | } |
| 31 | { |
| 32 | // longer strings |
| 33 | auto df = DfTest::Make(DfTest::MakeStrs("cbd", "bbd", "dbd", "ab")); |
| 34 | auto orig_dict = df.View(); |
| 35 | |
| 36 | auto new_df = DfTest::Make(DfTest::MakeStrs("cbd", "ab", "bbd")); |
| 37 | auto new_dict = new_df.View(); |
| 38 | |
| 39 | encoder.Recode(orig_dict, new_dict, new_df.MappingView()); |
| 40 | ASSERT_EQ(new_df.Mapping().size(), 3); |
| 41 | ASSERT_EQ(new_df.Mapping(), sol); |
| 42 | } |
| 43 | { |
| 44 | // Test error message. |
| 45 | auto df = DfTest::Make(DfTest::MakeStrs("cbd", "bbd", "dbd", "ab")); |
| 46 | auto orig_dict = df.View(); |
| 47 | |
| 48 | auto new_df = DfTest::Make(DfTest::MakeStrs("oops", "ab", "bbd")); |
| 49 | auto new_dict = new_df.View(); |
| 50 | ASSERT_THAT([&] { encoder.Recode(orig_dict, new_dict, new_df.MappingView()); }, |
| 51 | ::testing::ThrowsMessage<std::logic_error>(::testing::HasSubstr("`oops`"))); |
| 52 | } |
| 53 | { |
| 54 | // Multi-columns |
| 55 | auto df = DfTest::Make(DfTest::MakeStrs("cbd", "bbd", "dbd", "ab"), |
| 56 | DfTest::MakeStrs("b", "c", "a", "d")); |
| 57 | auto orig_dict = df.View(); |
| 58 | |
| 59 | auto new_df = |
| 60 | DfTest::Make(DfTest::MakeStrs("cbd", "ab", "bbd"), DfTest::MakeStrs("d", "a", "b")); |
| 61 | auto new_dict = new_df.View(); |
| 62 | |
| 63 | encoder.Recode(orig_dict, new_dict, new_df.MappingView()); |
| 64 | auto segs = new_df.Segment(); |
| 65 | auto beg = segs[0]; |
| 66 | auto end = segs[1]; |
| 67 | |
| 68 | auto sol0 = sol; |
| 69 | for (auto i = beg, k = 0; i < end; ++i, ++k) { |
| 70 | ASSERT_EQ(sol0[k], new_df.Mapping()[i]); |
| 71 | } |