Skip to content
Open
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: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,7 @@ CPack*
_CPack*
.cache/
*.deb

# test files
test/lib/googletest/googletest/generated/
jsonlogic_test
12 changes: 9 additions & 3 deletions src/operations/data/missing.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "json_logic.h"

#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#include "nlohmann/json.hpp"

Expand All @@ -17,7 +18,8 @@ namespace json_logic
"Expected 1 or more arguments, but received " + std::to_string(values.size())
);

std::set<std::string> missing_keys{};
std::vector<std::string> missing_keys{};
std::unordered_set<std::string> seen{};

for (const auto& value : values)
{
Expand All @@ -34,7 +36,11 @@ namespace json_logic
}
catch (...)
{
missing_keys.insert(key);
const auto [_, inserted] = seen.insert(key);
if (inserted)
{
missing_keys.push_back(key);
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/operations/data/missing_some.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "json_logic.h"

#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#include "nlohmann/json.hpp"

Expand All @@ -23,8 +24,9 @@ namespace json_logic
if (!search_values.is_array())
throw JsonLogicException(__FUNCTION__, "Second argument must be an array");

std::set<std::string> found_keys{};
std::set<std::string> missing_keys{};
std::vector<std::string> missing_keys{};
std::unordered_set<std::string> found_keys{};
std::unordered_set<std::string> seen_missing{};

for (const auto& value : search_values)
{
Expand All @@ -43,7 +45,11 @@ namespace json_logic
}
catch (...)
{
missing_keys.insert(key);
const auto [_, inserted] = seen_missing.insert(key);
if (inserted)
{
missing_keys.push_back(key);
}
}
}

Expand Down
52 changes: 47 additions & 5 deletions test/operations/data/missing.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <set>
#include <vector>

#include "gtest/gtest.h"

Expand Down Expand Up @@ -29,7 +29,7 @@ TEST_F(OperationDataMissing, SimpleKeys)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"b", "d"};
std::vector<std::string> expected_result{"b", "d"};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -52,7 +52,7 @@ TEST_F(OperationDataMissing, SimpleKeysAllPresent)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{};
std::vector<std::string> expected_result{};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -73,7 +73,7 @@ TEST_F(OperationDataMissing, SimpleKeysRepeatedKeys)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"b", "d"};
std::vector<std::string> expected_result{"b", "d"};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -100,6 +100,48 @@ TEST_F(OperationDataMissing, NestedKeys)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"a.y", "b.z"};
std::vector<std::string> expected_result{"a.y", "b.z"};
EXPECT_EQ(result, expected_result);
}

TEST_F(OperationDataMissing, OrderPreservationNonAlphabetic)
{
const auto logic = R"(
{
"missing": ["zebra", "apple", "banana", "dog", "cat"]
}
)"_json;

const auto data = R"(
{
"apple": 1,
"dog": 4
}
)"_json;

const auto result = json_logic_->Apply(logic, data);

std::vector<std::string> expected_result{"zebra", "banana", "cat"};
EXPECT_EQ(result, expected_result);
}

TEST_F(OperationDataMissing, OrderPreservationReversedKeys)
{
const auto logic = R"(
{
"missing": ["z", "y", "x", "w", "v"]
}
)"_json;

const auto data = R"(
{
"y": 2,
"w": 4
}
)"_json;

const auto result = json_logic_->Apply(logic, data);

std::vector<std::string> expected_result{"z", "x", "v"};
EXPECT_EQ(result, expected_result);
}
56 changes: 49 additions & 7 deletions test/operations/data/missing_some.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <set>
#include <vector>

#include "gtest/gtest.h"

Expand Down Expand Up @@ -29,7 +29,7 @@ TEST_F(OperationDataMissingSome, SimpleKeysFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{};
std::vector<std::string> expected_result{};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -50,7 +50,7 @@ TEST_F(OperationDataMissingSome, SimpleKeysNotFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"b", "d"};
std::vector<std::string> expected_result{"b", "d"};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -71,7 +71,7 @@ TEST_F(OperationDataMissingSome, SimpleRepeatedKeysFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{};
std::vector<std::string> expected_result{};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -92,7 +92,7 @@ TEST_F(OperationDataMissingSome, SimpleRepeatedKeysNotFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"b", "d"};
std::vector<std::string> expected_result{"b", "d"};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -119,7 +119,7 @@ TEST_F(OperationDataMissingSome, NestedKeysFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{};
std::vector<std::string> expected_result{};
EXPECT_EQ(result, expected_result);
}

Expand All @@ -146,6 +146,48 @@ TEST_F(OperationDataMissingSome, NestedKeysNotFoundMinimum)

const auto result = json_logic_->Apply(logic, data);

std::set<std::string> expected_result{"a.y", "b.z"};
std::vector<std::string> expected_result{"a.y", "b.z"};
EXPECT_EQ(result, expected_result);
}

TEST_F(OperationDataMissingSome, OrderPreservationNonAlphabetic)
{
const auto logic = R"(
{
"missing_some": [3, ["zebra", "dog", "apple", "banana", "cat"]]
}
)"_json;

const auto data = R"(
{
"dog": 4,
"apple": 1
}
)"_json;

const auto result = json_logic_->Apply(logic, data);

std::vector<std::string> expected_result{"zebra", "banana", "cat"};
EXPECT_EQ(result, expected_result);
}

TEST_F(OperationDataMissingSome, OrderPreservationReversedKeys)
{
const auto logic = R"(
{
"missing_some": [3, ["z", "y", "x", "w", "v"]]
}
)"_json;

const auto data = R"(
{
"y": 2,
"w": 4
}
)"_json;

const auto result = json_logic_->Apply(logic, data);

std::vector<std::string> expected_result{"z", "x", "v"};
EXPECT_EQ(result, expected_result);
}