From c8605cde8a81c38eb81054d9190e8a713dd5df41 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Tue, 14 May 2024 23:07:32 -0700 Subject: [PATCH] Fix headers search paths for nlohmann json in Core ML. (#3614) Summary: . Differential Revision: D57368772 --- .../coreml/runtime/test/DatabaseTests.mm | 4 +- .../runtime/test/InMemoryFileSystemTests.mm | 38 +++++++++---------- .../coreml/runtime/test/KeyValueStoreTests.mm | 22 +++++------ 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/backends/apple/coreml/runtime/test/DatabaseTests.mm b/backends/apple/coreml/runtime/test/DatabaseTests.mm index 9b89f20aa5a..1d66448852e 100644 --- a/backends/apple/coreml/runtime/test/DatabaseTests.mm +++ b/backends/apple/coreml/runtime/test/DatabaseTests.mm @@ -8,7 +8,7 @@ #import #import -#import +#import @interface DatabaseTests : XCTestCase @@ -58,7 +58,7 @@ - (void)testDatabaseQuery { XCTAssertTrue(insertStatement->bind_name("$value", std::string("1"), error)); XCTAssertTrue(insertStatement->execute(error)); XCTAssertTrue(database->get_row_count("TEST", error) == 1); - + auto query = database->prepare_statement("SELECT * FROM TEST", error); XCTAssertTrue(query != nullptr); XCTAssertTrue(query->step(error)); diff --git a/backends/apple/coreml/runtime/test/InMemoryFileSystemTests.mm b/backends/apple/coreml/runtime/test/InMemoryFileSystemTests.mm index a4ccbd94b68..226a42aaaaf 100644 --- a/backends/apple/coreml/runtime/test/InMemoryFileSystemTests.mm +++ b/backends/apple/coreml/runtime/test/InMemoryFileSystemTests.mm @@ -13,7 +13,7 @@ #import #import -#import +#import #import using json = nlohmann::json; @@ -25,11 +25,11 @@ inline Content(std::string identifier, std::string value) noexcept :identifier(std::move(identifier)), value(std::move(value)) {} - + inline Content() noexcept :identifier(""), value("") {} - + std::string identifier; std::string value; }; @@ -80,7 +80,7 @@ T from_memory_buffer(const std::shared_ptr& buffer) { for (size_t i = 0; i < length; ++i) { result += chars[rand() % (sizeof(chars) - 1)]; } - + return result; } @@ -178,12 +178,12 @@ - (void)testWriteItemAtPath { Content content("abc", "xyz"); std::shared_ptr buffer = to_memory_buffer(content); std::error_code error; - + XCTAssertTrue(fs.make_directory({"dir1"}, InMemoryFileSystem::Attributes(), false, error)); XCTAssertTrue(fs.make_file({"dir1", "content.json"}, buffer, InMemoryFileSystem::Attributes(), false /*overwrite*/, error)); XCTAssertTrue(fs.make_directory({"dir1", "dir2"}, InMemoryFileSystem::Attributes(), false, error)); XCTAssertTrue(fs.make_file({"dir1", "dir2", "content.json"}, buffer, InMemoryFileSystem::Attributes(), false /*overwrite*/, error)); - + NSURL *dirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:[NSUUID UUID].UUIDString]; NSFileManager *fm = [[NSFileManager alloc] init]; NSError *localError = nil; @@ -220,7 +220,7 @@ - (void)testCreationFromFileSystem { NSData *data = [NSData dataWithBytesNoCopy:buffer->data() length:buffer->size() freeWhenDone:NO]; XCTAssertTrue([data writeToURL:[dirURL URLByAppendingPathComponent:@"dir1/content.json"] atomically:YES]); XCTAssertTrue([data writeToURL:[dirURL URLByAppendingPathComponent:@"dir2/content.json"] atomically:YES]); - + std::filesystem::path dirPath(dirURL.path.UTF8String); std::error_code error; auto fs = InMemoryFileSystem::make_from_directory(dirPath, @@ -256,7 +256,7 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config { } XCTAssertTrue(fs.write_item_to_disk({}, dirURL.path.UTF8String, true, error)); } - + // Verify serialization. std::shared_ptr buffer = nullptr; { @@ -264,7 +264,7 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config { auto fs = InMemoryFileSystem::make_from_directory(dirURL.path.UTF8String, config.file_load_option, error); - + XCTAssertTrue(fs != nullptr); size_t length = inmemoryfs::get_buffer_size_for_serialization(*fs, {}, config.alignment); switch (config.file_load_option) { @@ -272,15 +272,15 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config { buffer = MemoryBuffer::make_using_mmap(length); break; } - + default: buffer = MemoryBuffer::make_using_malloc(length); break; } - + XCTAssertTrue(inmemoryfs::serialize(*fs, {}, config.alignment, buffer->data(), error)); } - + // Verify de-serialization. { auto fs = inmemoryfs::make_from_buffer(buffer); @@ -290,7 +290,7 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config { XCTAssertEqual(from_memory_buffer(fs->get_file_content({"test", "dir", content.identifier}, error)), content); } } - + [fm removeItemAtURL:dirURL error:nil]; } @@ -332,7 +332,7 @@ - (void)testSerde { .file_base_length = 100, .alignment = 2 * (size_t)getpagesize(), }); - + for (const auto& config : configs) { [self _testSerdeWithConfig:config]; } @@ -349,7 +349,7 @@ - (void)testReadJSONObject { auto j = json::parse(object.value().begin(), object.value().end()); XCTAssertEqual(j["x"], 1, "The value must match"); } - + { std::stringstream ss; std::string fragment("{\"x\" : 1"); @@ -357,8 +357,8 @@ - (void)testReadJSONObject { auto object = executorchcoreml::json::read_object_from_stream(ss); XCTAssertFalse(object.has_value(), "There is no closing brace, `read_json_object` must return nullopt"); } - - + + { std::stringstream ss; std::string fragment("{\"x\" : \"\\\"1\"}xyz"); @@ -369,7 +369,7 @@ - (void)testReadJSONObject { std::string value = j["x"]; XCTAssertEqual(value, std::string("\"1"), "The value must match"); } - + { std::stringstream ss; std::string fragment("{sdhalskjks}"); @@ -384,7 +384,7 @@ - (void)testReadJSONObject { } XCTAssertNotEqual(eptr, nullptr, "Parsing invalid json object must throw an exception"); } - + } @end diff --git a/backends/apple/coreml/runtime/test/KeyValueStoreTests.mm b/backends/apple/coreml/runtime/test/KeyValueStoreTests.mm index 81a667bc375..4d113efa43a 100644 --- a/backends/apple/coreml/runtime/test/KeyValueStoreTests.mm +++ b/backends/apple/coreml/runtime/test/KeyValueStoreTests.mm @@ -9,7 +9,7 @@ #import #import -#import +#import namespace { using json = nlohmann::json; @@ -24,11 +24,11 @@ inline Entry(std::string identifier, size_t count) noexcept :identifier(std::move(identifier)), count(count) {} - + inline Entry() noexcept :identifier(""), count(0) {} - + inline std::string to_json_string() const noexcept { json j; to_json(j, *this); @@ -36,12 +36,12 @@ inline Entry() noexcept ss << j; return ss.str(); } - + inline void from_json_string(const std::string& json_string) noexcept { auto j = json::parse(json_string); from_json(j, *this); } - + std::string identifier; size_t count; }; @@ -110,12 +110,12 @@ - (void)testJSONKeyValueStore { std::error_code error; auto database = Database::make_inmemory(Database::SynchronousMode::Normal, 100, error); auto store = JSONKeyValueStore::make(std::move(database), "test", error); - + XCTAssertTrue(store->put(1, Entry("1", 1), error)); auto entry1 = store->get(1, error); XCTAssertTrue(entry1.value().count == 1); XCTAssertTrue(entry1.value().identifier == "1"); - + XCTAssertTrue(store->put(2, Entry("2", 2), error)); auto entry2 = store->get(2, error); XCTAssertTrue(entry2.value().count == 2); @@ -134,7 +134,7 @@ - (void)testKVStoreTransactionCommit { // Commit the transaction. return true; }, Database::TransactionBehavior::Immediate, error)); - + XCTAssertTrue(store->size(error) == 2); } @@ -150,7 +150,7 @@ - (void)testKVStoreTransactionRollback { // Rollback the transaction. return false; }, Database::TransactionBehavior::Immediate, error)); - + XCTAssertTrue(store->size(error) == 0); } @@ -173,7 +173,7 @@ - (void)testKVStoreGetKeysSortedByAccessTime { // 1 is accessed first then 2 and then 3 XCTAssertTrue(keys == (std::vector{1, 2, 3})); } - + { std::vector keys; XCTAssertTrue(store->get_keys_sorted_by_access_time([&keys](int key) { @@ -210,7 +210,7 @@ - (void)testKVStoreGetKeysSortedByAccessCount { // 3 is accessed 1 time, 2 is accessed 2 times, and 1 is accessed 3 times. XCTAssertTrue(keys == (std::vector{3, 2, 1})); } - + { std::vector keys; XCTAssertTrue(store->get_keys_sorted_by_access_count([&keys](int key) {