-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompTest.py
More file actions
executable file
·112 lines (93 loc) · 3.36 KB
/
CompTest.py
File metadata and controls
executable file
·112 lines (93 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# CompTest.py imports CompTree.py as a
# library, to verify correct operation
# M. Williamsen 26 November 2023
import json, sys, datetime, os
import CompTree as ct
# --------------------------------------------
# open test input and output files
try:
tstFileName = 'TestFiles/aa.json'
with open(tstFileName, 'r') as tstFile:
tstData = json.load(tstFile)
except ValueError as e:
print ('Tst file {} is not valid JSON'.format(tstFileName), file = sys.stderr)
sys.exit (-1)
try:
refFileName = 'TestFiles/bb.json'
with open(refFileName, 'r') as refFile:
refData = json.load(refFile)
except ValueError as e:
print ('Ref file {} is not valid JSON'.format(refFileName), file = sys.stderr)
sys.exit (-1)
try:
outFileName = 'Tst.json'
outFile = open(outFileName, 'w')
except IOError as e:
print ('Failed to open output file: {}'.format(outFileName), file = sys.stderr)
sys.exit(-1)
# place timestamp at start of file
print ('{{"timestamp":"{}"'.format(datetime.datetime.now()), file = outFile)
# --------------------------------------------
# treeCompare
# set delta for floating point numbers
ct.errorDelta = 1e-3
errors = []
print (ct.treeCompare(tstData, refData, errors))
print (',"treeCompare1":{}'.format(json.dumps(errors)), file = outFile)
errors = []
print (ct.treeCompare(refData, tstData, errors))
print (',"treeCompare2":{}'.format(json.dumps(errors)), file = outFile)
errors = []
print (ct.treeCompare([], tstData, errors))
print (',"treeCompare3":{}'.format(json.dumps(errors)), file = outFile)
errors = []
print (ct.treeCompare({}, tstData, errors))
print (',"treeCompare4":{}'.format(json.dumps(errors)), file = outFile)
# --------------------------------------------
# open test input files for following tests
try:
tstFileName = 'TestFiles/cc.json'
with open(tstFileName, 'r') as tstFile:
tstData = json.load(tstFile)
except ValueError as e:
print ('Tst file {} is not valid JSON'.format(tstFileName), file = sys.stderr)
sys.exit (-1)
# --------------------------------------------
# locateKey
rslt = ct.locateKey('one', tstData)
print (',"locateKey1":{}'.format(json.dumps(list(rslt))), file = outFile)
rslt = ct.locateKey('five', tstData)
print (',"locateKey2":{}'.format(json.dumps(list(rslt))), file = outFile)
# --------------------------------------------
# locatePair
rslt = ct.locatePair('one', 1, tstData)
print (',"locatePair1":{}'.format(json.dumps(list(rslt))), file = outFile)
rslt = ct.locatePair('five', 'aa', tstData)
print (',"locatePair2":{}'.format(json.dumps(list(rslt))), file = outFile)
# --------------------------------------------
# locateTree
sub = {
"one": 1,
"two": 22,
"three": 23
}
match = ct.locateTree(sub, tstData)
print (',"locateTree1":{}'.format(json.dumps(list(match))), file = outFile)
sub = {
"three": 23,
"four": 25
}
match = ct.locateTree(sub, tstData)
print (',"locateTree2":{}'.format(json.dumps(list(match))), file = outFile)
sub = {
"three": 24,
"four": 25
}
match = ct.locateTree(sub, tstData)
print (',"locateTree3":{}}}'.format(json.dumps(list(match))), file = outFile)
# --------------------------------------------
# Compare test output file with reference file
# Test passes if console shows an empty array
outFile.close()
os.system('python3 CompTree.py Ref.json Tst.json')