|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +// @lint-ignore-every LICENSELINT |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include <pytorch/tokenizers/pre_tokenizer.h> |
| 12 | + |
| 13 | +using namespace tokenizers; |
| 14 | + |
| 15 | +TEST(IsolatedBehaviorTest, BasicIsolatedBehavior) { |
| 16 | + // Test the example from the comment: "the-final--countdown" -> ["the", "-", |
| 17 | + // "final", "-", "-", "countdown"] |
| 18 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 19 | + std::string input = "the-final--countdown"; |
| 20 | + std::vector<std::string> expected = { |
| 21 | + "the", "-", "final", "-", "-", "countdown"}; |
| 22 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 23 | + |
| 24 | + EXPECT_EQ(result.size(), expected.size()); |
| 25 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 26 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +TEST(IsolatedBehaviorTest, SingleDelimiter) { |
| 31 | + // Test with single delimiter |
| 32 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 33 | + std::string input = "hello-world"; |
| 34 | + std::vector<std::string> expected = {"hello", "-", "world"}; |
| 35 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 36 | + |
| 37 | + EXPECT_EQ(result.size(), expected.size()); |
| 38 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 39 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +TEST(IsolatedBehaviorTest, NoDelimiters) { |
| 44 | + // Test with no delimiters |
| 45 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 46 | + std::string input = "helloworld"; |
| 47 | + std::vector<std::string> expected = {"helloworld"}; |
| 48 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 49 | + |
| 50 | + EXPECT_EQ(result.size(), expected.size()); |
| 51 | + EXPECT_EQ(result[0], expected[0]); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(IsolatedBehaviorTest, DelimiterAtStart) { |
| 55 | + // Test with delimiter at start |
| 56 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 57 | + std::string input = "-hello"; |
| 58 | + std::vector<std::string> expected = {"-", "hello"}; |
| 59 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 60 | + |
| 61 | + EXPECT_EQ(result.size(), expected.size()); |
| 62 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 63 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +TEST(IsolatedBehaviorTest, DelimiterAtEnd) { |
| 68 | + // Test with delimiter at end |
| 69 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 70 | + std::string input = "hello-"; |
| 71 | + std::vector<std::string> expected = {"hello", "-"}; |
| 72 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 73 | + |
| 74 | + EXPECT_EQ(result.size(), expected.size()); |
| 75 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 76 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +TEST(IsolatedBehaviorTest, OnlyDelimiters) { |
| 81 | + // Test with only delimiters |
| 82 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 83 | + std::string input = "---"; |
| 84 | + std::vector<std::string> expected = {"-", "-", "-"}; |
| 85 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 86 | + |
| 87 | + EXPECT_EQ(result.size(), expected.size()); |
| 88 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 89 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +TEST(IsolatedBehaviorTest, SpaceDelimiter) { |
| 94 | + // Test with space as delimiter |
| 95 | + RegexPreTokenizer tokenizer(" ", true, "Isolated"); |
| 96 | + std::string input = "hello world test"; |
| 97 | + std::vector<std::string> expected = {"hello", " ", "world", " ", "test"}; |
| 98 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 99 | + |
| 100 | + EXPECT_EQ(result.size(), expected.size()); |
| 101 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 102 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +TEST(IsolatedBehaviorTest, JSONConfig) { |
| 107 | + // Test with JSON configuration |
| 108 | + nlohmann::json config = { |
| 109 | + {"type", "Split"}, |
| 110 | + {"pattern", {{"String", "-"}}}, |
| 111 | + {"behavior", "Isolated"}, |
| 112 | + {"invert", false}}; |
| 113 | + |
| 114 | + PreTokenizerConfig pre_config; |
| 115 | + pre_config.parse_json(config); |
| 116 | + auto tokenizer = pre_config.create(); |
| 117 | + |
| 118 | + std::string input = "the-final--countdown"; |
| 119 | + std::vector<std::string> expected = { |
| 120 | + "the", "-", "final", "-", "-", "countdown"}; |
| 121 | + std::vector<std::string> result = tokenizer->pre_tokenize(input); |
| 122 | + |
| 123 | + EXPECT_EQ(result.size(), expected.size()); |
| 124 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 125 | + EXPECT_EQ(result[i], expected[i]) << "Mismatch at index " << i; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +TEST(IsolatedBehaviorTest, EmptyInput) { |
| 130 | + // Test with empty input |
| 131 | + RegexPreTokenizer tokenizer("-", true, "Isolated"); |
| 132 | + std::string input = ""; |
| 133 | + std::vector<std::string> result = tokenizer.pre_tokenize(input); |
| 134 | + |
| 135 | + EXPECT_EQ(result.size(), 1); |
| 136 | + EXPECT_EQ(result[0], ""); |
| 137 | +} |
0 commit comments