66 * LICENSE file in the root directory of this source tree.
77 */
88
9+ #ifdef EXECUTORCH_FB_BUCK
10+ #include < TestResourceUtils/TestResourceUtils.h>
11+ #endif
912#include < executorch/extension/llm/tokenizer/tiktoken.h>
1013#include < executorch/runtime/platform/runtime.h>
1114#include < gmock/gmock.h>
1215#include < gtest/gtest.h>
13- #include < sstream>
1416#include < vector>
1517
1618using namespace ::testing;
@@ -47,6 +49,15 @@ static inline std::unique_ptr<std::vector<std::string>> _get_special_tokens() {
4749 }
4850 return special_tokens;
4951}
52+
53+ static inline std::string _get_resource_path (const std::string& name) {
54+ #ifdef EXECUTORCH_FB_BUCK
55+ return facebook::xplat::testing::getPathForTestResource (" resources/" + name);
56+ #else
57+ return std::getenv (" RESOURCES_PATH" ) + std::string (" /" ) + name;
58+ #endif
59+ }
60+
5061} // namespace
5162
5263class TiktokenExtensionTest : public Test {
@@ -55,8 +66,7 @@ class TiktokenExtensionTest : public Test {
5566 executorch::runtime::runtime_init ();
5667 tokenizer_ = std::make_unique<Tiktoken>(
5768 _get_special_tokens (), kBOSTokenIndex , kEOSTokenIndex );
58- modelPath_ = std::getenv (" RESOURCES_PATH" ) +
59- std::string (" /test_tiktoken_tokenizer.model" );
69+ modelPath_ = _get_resource_path (" test_tiktoken_tokenizer.model" );
6070 }
6171
6272 std::unique_ptr<Tokenizer> tokenizer_;
@@ -144,44 +154,36 @@ TEST_F(TiktokenExtensionTest, ConstructionWithInvalidEOSIndex) {
144154}
145155
146156TEST_F (TiktokenExtensionTest, LoadWithInvalidPath) {
147- auto invalidModelPath =
148- std::getenv (" RESOURCES_PATH" ) + std::string (" /nonexistent.model" );
149-
150- Error res = tokenizer_->load (invalidModelPath.c_str ());
157+ auto invalidModelPath = " ./nonexistent.model" ;
158+ Error res = tokenizer_->load (invalidModelPath);
151159 EXPECT_EQ (res, Error::InvalidArgument);
152160}
153161
154162TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithInvalidRank) {
155- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
156- std::string (" /test_tiktoken_invalid_rank.model" );
157-
163+ auto invalidModelPath =
164+ _get_resource_path (" test_tiktoken_invalid_rank.model" );
158165 Error res = tokenizer_->load (invalidModelPath.c_str ());
159166
160167 EXPECT_EQ (res, Error::InvalidArgument);
161168}
162169
163170TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithInvalidBase64) {
164- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
165- std::string (" /test_tiktoken_invalid_base64.model" );
166-
171+ auto invalidModelPath =
172+ _get_resource_path (" test_tiktoken_invalid_base64.model" );
167173 Error res = tokenizer_->load (invalidModelPath.c_str ());
168174
169175 EXPECT_EQ (res, Error::InvalidArgument);
170176}
171177
172178TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithNoSpace) {
173- auto invalidModelPath = std::getenv (" RESOURCES_PATH" ) +
174- std::string (" /test_tiktoken_no_space.model" );
175-
179+ auto invalidModelPath = _get_resource_path (" test_tiktoken_no_space.model" );
176180 Error res = tokenizer_->load (invalidModelPath.c_str ());
177181
178182 EXPECT_EQ (res, Error::InvalidArgument);
179183}
180184
181185TEST_F (TiktokenExtensionTest, LoadTiktokenFileWithBPEFile) {
182- auto invalidModelPath =
183- std::getenv (" RESOURCES_PATH" ) + std::string (" /test_bpe_tokenizer.bin" );
184-
186+ auto invalidModelPath = _get_resource_path (" test_bpe_tokenizer.bin" );
185187 Error res = tokenizer_->load (invalidModelPath.c_str ());
186188
187189 EXPECT_EQ (res, Error::InvalidArgument);
0 commit comments