json: fix bounds tests

This commit is contained in:
ochafik 2024-05-19 00:52:34 +01:00
parent 5a86c6f0e2
commit 431edb8e7b
3 changed files with 49 additions and 9 deletions

View file

@ -202,6 +202,7 @@ static void _generate_min_max_int(int min_value, int max_value, std::stringstrea
} }
auto sub_len = from.length() - i - 1; auto sub_len = from.length() - i - 1;
if (sub_len > 0) { if (sub_len > 0) {
out << "(";
auto from_sub = from.substr(i + 1); auto from_sub = from.substr(i + 1);
auto to_sub = to.substr(i + 1); auto to_sub = to.substr(i + 1);
auto sub_zeros = repeat("0", sub_len); auto sub_zeros = repeat("0", sub_len);
@ -235,6 +236,7 @@ static void _generate_min_max_int(int min_value, int max_value, std::stringstrea
out << " "; out << " ";
uniform_range(sub_zeros, to_sub); uniform_range(sub_zeros, to_sub);
} }
out << ")";
} else { } else {
out << "[" << from[i] << "-" << to[i] << "]"; out << "[" << from[i] << "-" << to[i] << "]";
} }

View file

@ -150,6 +150,29 @@ static void test_simple_grammar() {
"100000000000000000000000000000000", "100000000000000000000000000000000",
} }
); );
test_schema(
"simple min -1 max 1",
R"""({
"type": "integer",
"minimum": -1,
"maximum": 1
})""",
// Passing strings
{
"-1",
"0",
"1",
},
// Failing strings
{
"-11",
"-10",
"-2",
"2",
"10",
"11",
}
);
test_schema( test_schema(
"simple min -123 max 42", "simple min -123 max 42",
R"""({ R"""({

View file

@ -166,7 +166,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"maximum": 30 "maximum": 30
})""", })""",
R"""( R"""(
root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | [1-2] [0-9] | [3] "0") space root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-2] [0-9] | [3] "0")) space
space ::= " "? space ::= " "?
)""" )"""
}); });
@ -205,7 +205,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"maximum": 100 "maximum": 100
})""", })""",
R"""( R"""(
root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | [1-8] [0-9] | [9] [0-9] | "100") space root ::= ("-" [1-9] [0-9]{0,15} | [0-9] | ([1-8] [0-9] | [9] [0-9]) | "100") space
space ::= " "? space ::= " "?
)""" )"""
}); });
@ -219,7 +219,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"maximum": 23 "maximum": 23
})""", })""",
R"""( R"""(
root ::= ([0-9] | [1] [0-9] | [2] [0-3]) space root ::= ([0-9] | ([1] [0-9] | [2] [0-3])) space
space ::= " "? space ::= " "?
)""" )"""
}); });
@ -233,7 +233,7 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"maximum": 300 "maximum": 300
})""", })""",
R"""( R"""(
root ::= ([1] ([5-9]) | [2-9] [0-9] | [1-2] [0-9]{2} | [3] "00") space root ::= (([1] ([5-9]) | [2-9] [0-9]) | ([1-2] [0-9]{2} | [3] "00")) space
space ::= " "? space ::= " "?
)""" )"""
}); });
@ -247,7 +247,21 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
"maximum": 30 "maximum": 30
})""", })""",
R"""( R"""(
root ::= ([5-9] | [1-2] [0-9] | [3] "0") space root ::= ([5-9] | ([1-2] [0-9] | [3] "0")) space
space ::= " "?
)"""
});
test({
SUCCESS,
"min -123 max 42",
R"""({
"type": "integer",
"minimum": -123,
"maximum": 42
})""",
R"""(
root ::= ("-" ([0-9] | ([1-8] [0-9] | [9] [0-9]) | "1" ([0-1] [0-9] | [2] [0-3])) | [0-9] | ([1-3] [0-9] | [4] [0-2])) space
space ::= " "? space ::= " "?
)""" )"""
}); });
@ -582,14 +596,14 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
R"""({ R"""({
"items": { "items": {
"type": "integer", "type": "integer",
"minimum": -122, "minimum": -12,
"maximum": 207 "maximum": 207
}, },
"minItems": 3, "minItems": 3,
"maxItems": 5 "maxItems": 5
})""", })""",
R"""( R"""(
item ::= ("-" ([0-9] | [1-8] [0-9] | [9] [0-9] | "1" [0-1] [0-9] | [2] [0-2]) | [0-9] | [1-8] [0-9] | [9] [0-9] | [1] [0-9]{2} | [2] "0" [0-7]) space item ::= ("-" ([0-9] | "1" [0-2]) | [0-9] | ([1-8] [0-9] | [9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
root ::= "[" space item ("," space item){2,4} "]" space root ::= "[" space item ("," space item){2,4} "]" space
space ::= " "? space ::= " "?
)""" )"""
@ -601,14 +615,14 @@ static void test_all(const std::string & lang, std::function<void(const TestCase
R"""({ R"""({
"items": { "items": {
"type": "integer", "type": "integer",
"minimum": 122, "minimum": 12,
"maximum": 207 "maximum": 207
}, },
"minItems": 3, "minItems": 3,
"maxItems": 5 "maxItems": 5
})""", })""",
R"""( R"""(
item ::= ([1] ([2] ([2-9]) | [3-9] [0-9]) | [2] "0" [0-7]) space item ::= (([1] ([2-9]) | [2-9] [0-9]) | ([1] [0-9]{2} | [2] "0" [0-7])) space
root ::= "[" space item ("," space item){2,4} "]" space root ::= "[" space item ("," space item){2,4} "]" space
space ::= " "? space ::= " "?
)""" )"""
@ -1094,6 +1108,7 @@ int main() {
} }
}); });
if (getenv("LLAMA_PYTHON_AVAILABLE") || (std::system("python --version") == 0)) {
test_all("Python", [](const TestCase & tc) { test_all("Python", [](const TestCase & tc) {
write("test-json-schema-input.tmp", tc.schema); write("test-json-schema-input.tmp", tc.schema);
tc.verify_status(std::system( tc.verify_status(std::system(