2017-01-11 15:28:41 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
import os, itertools, urllib
|
|
|
|
|
|
|
|
def get_ch_answer(query):
|
2017-04-05 11:52:23 +00:00
|
|
|
return urllib.urlopen('http://localhost:8123', data=query).read()
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
def check_answers(query, answer):
|
2017-04-05 11:52:23 +00:00
|
|
|
ch_answer = get_ch_answer(query)
|
|
|
|
if ch_answer.strip() != answer.strip():
|
|
|
|
print("FAIL on query:", query)
|
|
|
|
print("Expected answer:", answer)
|
|
|
|
print("Fetched answer :", ch_answer)
|
|
|
|
exit(-1)
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
def get_values():
|
2017-04-05 11:52:23 +00:00
|
|
|
values = [0, 1, -1]
|
|
|
|
for bits in [8, 16, 32, 64]:
|
|
|
|
values += [2**bits, 2**bits - 1]
|
|
|
|
values += [2**(bits-1) - 1, 2**(bits-1), 2**(bits-1) + 1]
|
|
|
|
values += [-2**(bits-1) - 1, -2**(bits-1), -2**(bits-1) + 1]
|
|
|
|
return values
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
def is_valid_integer(x):
|
2017-04-05 11:52:23 +00:00
|
|
|
return -2**63 <= x and x <= 2**64-1
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST_WITH_CASTING=True
|
|
|
|
GENERATE_TEST_FILES=False
|
|
|
|
|
|
|
|
TYPES = {
|
2017-04-05 11:52:23 +00:00
|
|
|
"UInt8" : { "bits" : 8, "sign" : False, "float" : False },
|
|
|
|
"Int8" : { "bits" : 8, "sign" : True, "float" : False },
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
"UInt16": { "bits" : 16, "sign" : False, "float" : False },
|
|
|
|
"Int16" : { "bits" : 16, "sign" : True, "float" : False },
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
"UInt32": { "bits" : 32, "sign" : False, "float" : False },
|
|
|
|
"Int32" : { "bits" : 32, "sign" : True, "float" : False },
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
"UInt64": { "bits" : 64, "sign" : False, "float" : False },
|
|
|
|
"Int64" : { "bits" : 64, "sign" : True, "float" : False }
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
#"Float32" : { "bits" : 32, "sign" : True, "float" : True },
|
|
|
|
#"Float64" : { "bits" : 64, "sign" : True, "float" : True }
|
2017-01-11 15:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def inside_range(value, type_name):
|
2017-04-05 11:52:23 +00:00
|
|
|
bits = TYPES[type_name]["bits"]
|
|
|
|
signed = TYPES[type_name]["sign"]
|
|
|
|
is_float = TYPES[type_name]["float"]
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
if is_float:
|
|
|
|
return True
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
if signed:
|
|
|
|
return -2**(bits-1) <= value and value <= 2**(bits-1) - 1
|
|
|
|
else:
|
|
|
|
return 0 <= value and value <= 2**bits - 1
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_operators(v1, v2, v1_passed, v2_passed):
|
2017-04-05 11:52:23 +00:00
|
|
|
query_str = "{v1} = {v2}, {v1} != {v2}, {v1} < {v2}, {v1} <= {v2}, {v1} > {v2}, {v1} >= {v2},\t".format(v1=v1_passed, v2=v2_passed)
|
|
|
|
query_str += "{v1} = {v2}, {v1} != {v2}, {v1} < {v2}, {v1} <= {v2}, {v1} > {v2}, {v1} >= {v2} ".format(v1=v2_passed, v2=v1_passed)
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
answers = [v1 == v2, v1 != v2, v1 < v2, v1 <= v2, v1 > v2, v1 >= v2]
|
|
|
|
answers += [v2 == v1, v2 != v1, v2 < v1, v2 <= v1, v2 > v1, v2 >= v1]
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
answers_str = "\t".join([str(int(x)) for x in answers])
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
return (query_str, answers_str)
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
VALUES = [x for x in get_values() if is_valid_integer(x)]
|
|
|
|
|
|
|
|
def test_pair(v1, v2):
|
2017-04-05 11:52:23 +00:00
|
|
|
query = "SELECT {}, {}, ".format(v1, v2)
|
|
|
|
answers = "{}\t{}\t".format(v1, v2)
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
q, a = test_operators(v1, v2, str(v1), str(v2))
|
|
|
|
query += q
|
|
|
|
answers += a
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
if TEST_WITH_CASTING:
|
|
|
|
for t1 in TYPES.iterkeys():
|
|
|
|
if inside_range(v1, t1):
|
|
|
|
for t2 in TYPES.iterkeys():
|
|
|
|
if inside_range(v2, t2):
|
|
|
|
q, a = test_operators(v1, v2, 'to{}({})'.format(t1, v1), 'to{}({})'.format(t2, v2))
|
|
|
|
query += ', ' + q
|
|
|
|
answers += "\t" + a
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
check_answers(query, answers)
|
|
|
|
return query, answers
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
VALUES_INT = [0, -1, 1, 2**64-1, 2**63, -2**63, 2**63-1, 2**51, 2**52, 2**53-1, 2**53, 2**53+1, 2**53+2, -2**53+1, -2**53, -2**53-1, -2**53-2, 2*52, -2**52]
|
|
|
|
VALUES_FLOAT = [float(x) for x in VALUES_INT + [-0.5, 0.5, -1.5, 1.5, 2**53, 2**51 - 0.5, 2**51 + 0.5, 2**60, -2**60, -2**63 - 10000, 2**63 + 10000]]
|
|
|
|
|
|
|
|
def test_float_pair(i, f):
|
2017-04-05 11:52:23 +00:00
|
|
|
f_str = ("%.9f" % f)
|
|
|
|
query = "SELECT '{}', '{}', ".format(i, f_str)
|
|
|
|
answers = "{}\t{}\t".format(i, f_str)
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
q, a = test_operators(i, f, i, f_str)
|
|
|
|
query += q
|
|
|
|
answers += a
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
if TEST_WITH_CASTING:
|
|
|
|
for t1 in TYPES.iterkeys():
|
|
|
|
if inside_range(i, t1):
|
|
|
|
q, a = test_operators(i, f, 'to{}({})'.format(t1, i), f_str)
|
|
|
|
query += ', ' + q
|
|
|
|
answers += "\t" + a
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
check_answers(query, answers)
|
|
|
|
return query, answers
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-04-05 11:52:23 +00:00
|
|
|
if GENERATE_TEST_FILES:
|
|
|
|
base_name = '00411_accurate_number_comparison'
|
|
|
|
sql_file = open(base_name + '.sql', 'wt')
|
|
|
|
ref_file = open(base_name + '.reference', 'wt')
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
for (v1, v2) in itertools.combinations(VALUES, 2):
|
|
|
|
q, a = test_pair(v1, v2)
|
|
|
|
if GENERATE_TEST_FILES:
|
|
|
|
sql_file.write(q + ";\n")
|
|
|
|
ref_file.write(a + "\n")
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
for (i, f) in itertools.product(VALUES_INT, VALUES_FLOAT):
|
|
|
|
q, a = test_float_pair(i, f)
|
|
|
|
if GENERATE_TEST_FILES:
|
|
|
|
sql_file.write(q + ";\n")
|
|
|
|
ref_file.write(a + "\n")
|
2017-01-11 15:28:41 +00:00
|
|
|
|
2017-04-05 11:52:23 +00:00
|
|
|
print("PASSED")
|
2017-01-11 15:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-04-05 11:52:23 +00:00
|
|
|
main()
|