Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backends/apple/coreml/runtime/test/DatabaseTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#import <XCTest/XCTest.h>

#import <database.hpp>
#import <json.hpp>
#import <nlohmann/json.hpp>

@interface DatabaseTests : XCTestCase

Expand Down Expand Up @@ -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));
Expand Down
38 changes: 19 additions & 19 deletions backends/apple/coreml/runtime/test/InMemoryFileSystemTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#import <inmemory_filesystem_utils.hpp>
#import <memory_stream.hpp>
#import <json.hpp>
#import <nlohmann/json.hpp>
#import <json_util.hpp>

using json = nlohmann::json;
Expand All @@ -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;
};
Expand Down Expand Up @@ -80,7 +80,7 @@ T from_memory_buffer(const std::shared_ptr<MemoryBuffer>& buffer) {
for (size_t i = 0; i < length; ++i) {
result += chars[rand() % (sizeof(chars) - 1)];
}

return result;
}

Expand Down Expand Up @@ -178,12 +178,12 @@ - (void)testWriteItemAtPath {
Content content("abc", "xyz");
std::shared_ptr<MemoryBuffer> 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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -256,31 +256,31 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config {
}
XCTAssertTrue(fs.write_item_to_disk({}, dirURL.path.UTF8String, true, error));
}

// Verify serialization.
std::shared_ptr<MemoryBuffer> buffer = nullptr;
{
std::error_code error;
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) {
case InMemoryFileSystem::FileLoadOption::LazyMMap: {
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);
Expand All @@ -290,7 +290,7 @@ - (void)_testSerdeWithConfig:(SerdeVerificationConfig)config {
XCTAssertEqual(from_memory_buffer<Content>(fs->get_file_content({"test", "dir", content.identifier}, error)), content);
}
}

[fm removeItemAtURL:dirURL error:nil];
}

Expand Down Expand Up @@ -332,7 +332,7 @@ - (void)testSerde {
.file_base_length = 100,
.alignment = 2 * (size_t)getpagesize(),
});

for (const auto& config : configs) {
[self _testSerdeWithConfig:config];
}
Expand All @@ -349,16 +349,16 @@ - (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");
ss << fragment;
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");
Expand All @@ -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}");
Expand All @@ -384,7 +384,7 @@ - (void)testReadJSONObject {
}
XCTAssertNotEqual(eptr, nullptr, "Parsing invalid json object must throw an exception");
}

}

@end
22 changes: 11 additions & 11 deletions backends/apple/coreml/runtime/test/KeyValueStoreTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <XCTest/XCTest.h>

#import <json_key_value_store.hpp>
#import <json.hpp>
#import <nlohmann/json.hpp>

namespace {
using json = nlohmann::json;
Expand All @@ -24,24 +24,24 @@
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);
std::stringstream ss;
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;
};
Expand Down Expand Up @@ -110,12 +110,12 @@ - (void)testJSONKeyValueStore {
std::error_code error;
auto database = Database::make_inmemory(Database::SynchronousMode::Normal, 100, error);
auto store = JSONKeyValueStore<int, Entry>::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);
Expand All @@ -134,7 +134,7 @@ - (void)testKVStoreTransactionCommit {
// Commit the transaction.
return true;
}, Database::TransactionBehavior::Immediate, error));

XCTAssertTrue(store->size(error) == 2);
}

Expand All @@ -150,7 +150,7 @@ - (void)testKVStoreTransactionRollback {
// Rollback the transaction.
return false;
}, Database::TransactionBehavior::Immediate, error));

XCTAssertTrue(store->size(error) == 0);
}

Expand All @@ -173,7 +173,7 @@ - (void)testKVStoreGetKeysSortedByAccessTime {
// 1 is accessed first then 2 and then 3
XCTAssertTrue(keys == (std::vector<int>{1, 2, 3}));
}

{
std::vector<int> keys;
XCTAssertTrue(store->get_keys_sorted_by_access_time([&keys](int key) {
Expand Down Expand Up @@ -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<int>{3, 2, 1}));
}

{
std::vector<int> keys;
XCTAssertTrue(store->get_keys_sorted_by_access_count([&keys](int key) {
Expand Down