|
7 | 7 | */
|
8 | 8 |
|
9 | 9 | #include <executorch/runtime/backend/interface.h>
|
| 10 | +#include <executorch/runtime/backend/options.h> |
10 | 11 | #include <executorch/runtime/platform/runtime.h>
|
11 | 12 |
|
12 | 13 | #include <gtest/gtest.h>
|
| 14 | +#include <memory> |
13 | 15 |
|
14 | 16 | using namespace ::testing;
|
15 | 17 | using executorch::runtime::ArrayRef;
|
@@ -61,7 +63,9 @@ class MockBackend : public BackendInterface {
|
61 | 63 | int success_update = 0;
|
62 | 64 | for (const auto& backend_option : backend_options) {
|
63 | 65 | if (strcmp(backend_option.key, "Backend") == 0) {
|
64 |
| - if (std::holds_alternative<std::array<char, 256>>(backend_option.value)) { |
| 66 | + if (std::holds_alternative< |
| 67 | + std::array<char, executorch::runtime::kMaxOptionValueLength>>( |
| 68 | + backend_option.value)) { |
65 | 69 | // Store the value in our member variable
|
66 | 70 | const auto& arr = std::get<std::array<char, 256>>(backend_option.value);
|
67 | 71 | target_backend = std::string(arr.data());
|
@@ -283,3 +287,116 @@ TEST_F(BackendInterfaceUpdateTest, UpdateBetweenExecutes) {
|
283 | 287 | ASSERT_TRUE(mock_backend->target_backend.has_value());
|
284 | 288 | EXPECT_STREQ(mock_backend->target_backend.value().c_str(), "NPU");
|
285 | 289 | }
|
| 290 | + |
| 291 | +// Mock backend for testing |
| 292 | +class StubBackend : public BackendInterface { |
| 293 | + public: |
| 294 | + ~StubBackend() override = default; |
| 295 | + |
| 296 | + bool is_available() const override { |
| 297 | + return true; |
| 298 | + } |
| 299 | + |
| 300 | + Result<DelegateHandle*> init( |
| 301 | + BackendInitContext& context, |
| 302 | + FreeableBuffer* processed, |
| 303 | + ArrayRef<CompileSpec> compile_specs) const override { |
| 304 | + return nullptr; |
| 305 | + } |
| 306 | + |
| 307 | + Error execute( |
| 308 | + BackendExecutionContext& context, |
| 309 | + DelegateHandle* handle, |
| 310 | + EValue** args) const override { |
| 311 | + return Error::Ok; |
| 312 | + } |
| 313 | + |
| 314 | + Error get_option( |
| 315 | + BackendOptionContext& context, |
| 316 | + executorch::runtime::Span<executorch::runtime::BackendOption>& |
| 317 | + backend_options) override { |
| 318 | + // For testing purposes, just record that get_option was called |
| 319 | + // and verify the input parameters |
| 320 | + get_option_called = true; |
| 321 | + get_option_call_count++; |
| 322 | + last_get_option_size = backend_options.size(); |
| 323 | + |
| 324 | + // Verify that the expected option key is present and modify the value |
| 325 | + for (size_t i = 0; i < backend_options.size(); ++i) { |
| 326 | + if (strcmp(backend_options[i].key, "NumberOfThreads") == 0) { |
| 327 | + // Set the value to what was stored by set_option |
| 328 | + backend_options[i].value = last_num_threads; |
| 329 | + found_expected_key = true; |
| 330 | + break; |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + return Error::Ok; |
| 335 | + } |
| 336 | + |
| 337 | + Error set_option( |
| 338 | + BackendOptionContext& context, |
| 339 | + const executorch::runtime::Span<executorch::runtime::BackendOption>& |
| 340 | + backend_options) override { |
| 341 | + // Store the options for verification |
| 342 | + last_options_size = backend_options.size(); |
| 343 | + if (backend_options.size() > 0) { |
| 344 | + for (const auto& option : backend_options) { |
| 345 | + if (strcmp(option.key, "NumberOfThreads") == 0) { |
| 346 | + if (auto* val = std::get_if<int>(&option.value)) { |
| 347 | + last_num_threads = *val; |
| 348 | + } |
| 349 | + } |
| 350 | + } |
| 351 | + } |
| 352 | + return Error::Ok; |
| 353 | + } |
| 354 | + |
| 355 | + // Mutable for testing verification |
| 356 | + size_t last_options_size = 0; |
| 357 | + int last_num_threads = 0; |
| 358 | + bool get_option_called = false; |
| 359 | + int get_option_call_count = 0; |
| 360 | + size_t last_get_option_size = 0; |
| 361 | + bool found_expected_key = false; |
| 362 | +}; |
| 363 | + |
| 364 | +class BackendUpdateTest : public ::testing::Test { |
| 365 | + protected: |
| 366 | + void SetUp() override { |
| 367 | + // Since these tests cause ET_LOG to be called, the PAL must be initialized |
| 368 | + // first. |
| 369 | + executorch::runtime::runtime_init(); |
| 370 | + |
| 371 | + // Register the stub backend |
| 372 | + stub_backend = std::make_unique<StubBackend>(); |
| 373 | + Backend backend_config{"StubBackend", stub_backend.get()}; |
| 374 | + auto register_result = register_backend(backend_config); |
| 375 | + ASSERT_EQ(register_result, Error::Ok); |
| 376 | + } |
| 377 | + |
| 378 | + std::unique_ptr<StubBackend> stub_backend; |
| 379 | +}; |
| 380 | + |
| 381 | +// Test basic string functionality |
| 382 | +TEST_F(BackendUpdateTest, TestSetGetOption) { |
| 383 | + BackendOptions<1> backend_options; |
| 384 | + int new_num_threads = 4; |
| 385 | + backend_options.set_option("NumberOfThreads", new_num_threads); |
| 386 | + |
| 387 | + auto status = set_option("StubBackend", backend_options.view()); |
| 388 | + ASSERT_EQ(status, Error::Ok); |
| 389 | + |
| 390 | + // Set up the default option, which will be populuated by the get_option call |
| 391 | + BackendOption ref_backend_option{"NumberOfThreads", 0}; |
| 392 | + status = get_option("StubBackend", ref_backend_option); |
| 393 | + |
| 394 | + // Verify that the backend actually received the options |
| 395 | + ASSERT_TRUE( |
| 396 | + std::get<int>(ref_backend_option.value) == |
| 397 | + new_num_threads); |
| 398 | + |
| 399 | + // Verify that the backend actually update the options |
| 400 | + ASSERT_EQ(stub_backend->last_options_size, 1); |
| 401 | + ASSERT_EQ(stub_backend->last_num_threads, new_num_threads); |
| 402 | +} |
0 commit comments