-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomTest.py
99 lines (71 loc) · 2.64 KB
/
randomTest.py
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
import random
import sys
from node import Node
from fixedOrderEmbedder import makeDAG, rowMakeDAG
def randomTree(size, maxEdgeLength = 20):
nodes = []
root = Node(0, 0, [])
nodes.append(root)
for i in range(size-1):
parent = random.choice(nodes)
edgeLength = 2*(random.randint(2, maxEdgeLength))
newNode = Node(i+1, parent.height + edgeLength, [])
nodes.append(newNode)
parent.right(newNode)
return root
def printEdges(vertices):
def simple(v):
return "<" + str(v.node.dNode) + " " + str(v.node.height) + " - " + str(v.type.name) + ">"
for v in sorted(vertices, key= lambda v: (v.node.dNode, v.node.height, v.type) ):
print(simple(v), end=" --> \t")
for o in sorted(v.outgoing, key = lambda o: (o.node.height, o.type) ):
print(simple(o), end=" ")
print()
def checkCorrectness(root):
fast = list(sorted(makeDAG(root), key = lambda v : (v.node.dNode, v.node.height, v.type)))
slow = list(sorted(rowMakeDAG(root), key = lambda v : (v.node.dNode, v.node.height, v.type)))
if len(fast) != len(slow):
print("Different top lengths")
return False
for i in range(len(fast)):
eFast = list(sorted(fast[i].outgoing, key = lambda n: (n.node.dNode, n.node.height, n.type)))
eSlow = list(sorted(slow[i].outgoing, key = lambda n: (n.node.dNode, n.node.height, n.type)))
if len(eFast) != len(eSlow):
print("Different edge lengths " + str(i) + " --> " + str(fast[i].node.dNode))
print("\nFast")
printEdges(fast)
print("\nSlow")
printEdges(slow)
print("\n")
return False
for j in range(len(eFast)):
nFast = eFast[j]
nSlow = eSlow[j]
if nFast.node.height != nSlow.node.height or nFast.type != nSlow.type:
print("Different edges " + str(i) + ", " + str(j) )
print("\nFast")
printEdges(fast)
print("\nSlow")
printEdges(slow)
print("\n")
return False
return True
#random.seed(0)
size = 5
n = 1000000
tick = n/10
while True:
for s in range(5,10000):
print(s)
for i in range(n):
random.seed(i)
root = randomTree(s)
correct = checkCorrectness(root)
if not correct:
print("Bad s: " + str(s) + " i " + str(i))
root.printMe(0)
exit()
if i % tick == 0:
print(".", end=" ")
sys.stdout.flush()
print("\n\n\nFinished " + str(s))