|
| 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 | + |
| 9 | +#include <executorch/runtime/backend/backend_options.h> |
| 10 | +#include <executorch/runtime/platform/runtime.h> |
| 11 | + |
| 12 | +#include <gtest/gtest.h> |
| 13 | + |
| 14 | +using namespace ::testing; |
| 15 | +using executorch::runtime::BackendOptions; |
| 16 | +using executorch::runtime::BoolKey; |
| 17 | +using executorch::runtime::Error; |
| 18 | +using executorch::runtime::IntKey; |
| 19 | +using executorch::runtime::OptionKey; |
| 20 | +using executorch::runtime::StrKey; |
| 21 | + |
| 22 | +class BackendOptionsTest : public ::testing::Test { |
| 23 | + protected: |
| 24 | + void SetUp() override { |
| 25 | + // Since these tests cause ET_LOG to be called, the PAL must be initialized |
| 26 | + // first. |
| 27 | + executorch::runtime::runtime_init(); |
| 28 | + } |
| 29 | + BackendOptions<5> options; // Capacity of 5 for testing limits |
| 30 | +}; |
| 31 | + |
| 32 | +// Test basic string functionality |
| 33 | +TEST_F(BackendOptionsTest, HandlesStringOptions) { |
| 34 | + // Set and retrieve valid string |
| 35 | + options.set_option(StrKey("backend_type"), "GPU"); |
| 36 | + const char* result = nullptr; |
| 37 | + EXPECT_EQ(options.get_option(StrKey("backend_type"), result), Error::Ok); |
| 38 | + EXPECT_STREQ(result, "GPU"); |
| 39 | + |
| 40 | + // Update existing key |
| 41 | + options.set_option(StrKey("backend_type"), "CPU"); |
| 42 | + EXPECT_EQ(options.get_option(StrKey("backend_type"), result), Error::Ok); |
| 43 | + EXPECT_STREQ(result, "CPU"); |
| 44 | +} |
| 45 | + |
| 46 | +// Test boolean options |
| 47 | +TEST_F(BackendOptionsTest, HandlesBoolOptions) { |
| 48 | + options.set_option(BoolKey("debug"), true); |
| 49 | + bool debug = false; |
| 50 | + EXPECT_EQ(options.get_option(BoolKey("debug"), debug), Error::Ok); |
| 51 | + EXPECT_TRUE(debug); |
| 52 | + |
| 53 | + // Test false value |
| 54 | + options.set_option(BoolKey("verbose"), false); |
| 55 | + EXPECT_EQ(options.get_option(BoolKey("verbose"), debug), Error::Ok); |
| 56 | + EXPECT_FALSE(debug); |
| 57 | +} |
| 58 | + |
| 59 | +// Test integer options |
| 60 | +TEST_F(BackendOptionsTest, HandlesIntOptions) { |
| 61 | + options.set_option(IntKey("num_threads"), 256); |
| 62 | + int num_threads = 0; |
| 63 | + EXPECT_EQ(options.get_option(IntKey("num_threads"), num_threads), Error::Ok); |
| 64 | + EXPECT_EQ(num_threads, 256); |
| 65 | +} |
| 66 | + |
| 67 | +// Test error conditions |
| 68 | +TEST_F(BackendOptionsTest, HandlesErrors) { |
| 69 | + // Non-existent key |
| 70 | + bool dummy_bool; |
| 71 | + EXPECT_EQ( |
| 72 | + options.get_option(BoolKey("missing"), dummy_bool), Error::NotFound); |
| 73 | + |
| 74 | + // Type mismatch |
| 75 | + options.set_option(IntKey("threshold"), 100); |
| 76 | + const char* dummy_str = nullptr; |
| 77 | + EXPECT_EQ( |
| 78 | + options.get_option(StrKey("threshold"), dummy_str), |
| 79 | + Error::InvalidArgument); |
| 80 | + |
| 81 | + // Null value handling |
| 82 | + options.set_option(StrKey("nullable"), static_cast<const char*>(nullptr)); |
| 83 | + EXPECT_EQ(options.get_option(StrKey("nullable"), dummy_str), Error::Ok); |
| 84 | + EXPECT_EQ(dummy_str, nullptr); |
| 85 | +} |
| 86 | + |
| 87 | +// Test capacity limits |
| 88 | +TEST_F(BackendOptionsTest, HandlesCapacity) { |
| 89 | + // Use persistent storage for keys |
| 90 | + std::vector<std::string> keys = {"key0", "key1", "key2", "key3", "key4"}; |
| 91 | + |
| 92 | + // Fill to capacity with persistent keys |
| 93 | + for (int i = 0; i < 5; i++) { |
| 94 | + options.set_option(IntKey(keys[i].c_str()), i); |
| 95 | + } |
| 96 | + |
| 97 | + // Verify all exist |
| 98 | + int value; |
| 99 | + for (int i = 0; i < 5; i++) { |
| 100 | + EXPECT_EQ(options.get_option(IntKey(keys[i].c_str()), value), Error::Ok); |
| 101 | + EXPECT_EQ(value, i); |
| 102 | + } |
| 103 | + |
| 104 | + // Add beyond capacity - should fail |
| 105 | + const char* overflow_key = "overflow"; |
| 106 | + options.set_option(IntKey(overflow_key), 99); |
| 107 | + EXPECT_EQ(options.get_option(IntKey(overflow_key), value), Error::NotFound); |
| 108 | + |
| 109 | + // Update existing within capacity |
| 110 | + options.set_option(IntKey(keys[2].c_str()), 222); |
| 111 | + EXPECT_EQ(options.get_option(IntKey(keys[2].c_str()), value), Error::Ok); |
| 112 | + EXPECT_EQ(value, 222); |
| 113 | +} |
| 114 | + |
| 115 | +// Test type-specific keys |
| 116 | +TEST_F(BackendOptionsTest, EnforcesKeyTypes) { |
| 117 | + // Same key name - later set operations overwrite earlier ones |
| 118 | + options.set_option(BoolKey("flag"), true); |
| 119 | + options.set_option(IntKey("flag"), 123); // Overwrites the boolean entry |
| 120 | + |
| 121 | + bool bval; |
| 122 | + int ival; |
| 123 | + |
| 124 | + // Boolean get should fail - type was overwritten to INT |
| 125 | + EXPECT_EQ(options.get_option(BoolKey("flag"), bval), Error::InvalidArgument); |
| 126 | + |
| 127 | + // Integer get should succeed with correct value |
| 128 | + EXPECT_EQ(options.get_option(IntKey("flag"), ival), Error::Ok); |
| 129 | + EXPECT_EQ(ival, 123); |
| 130 | +} |
0 commit comments