""" The very basics of the Set class are tested by TestSetClass in CobraLang.cobra Here some additional tests are performed: * time consuming tests * language level tests See Workspace/Tests/300-type-inference/900-literals for literal sets and other collections. """ class Program def main .testForExpr .testInSpeed def testForExpr s = Set() s.add(1) s.add(2) assert (for n in s get n * 2) == [2, 4] def testInSpeed s = Set() t = List() # test that 'in' is fast x = 10000 for i in x s.add(i) t.add(i) x -= 1 reps = 1000 start = DateTime.now for n in reps assert x in s assert x in s assert x in s assert x in s assert x in s setTest = DateTime.now.subtract(start).totalSeconds start = DateTime.now for n in reps assert x in t assert x in t assert x in t assert x in t assert x in t listTest = DateTime.now.subtract(start).totalSeconds # trace setTest, listTest ratio = listTest / setTest # 274.807564698076 # trace ratio assert ratio > 15, [listTest, setTest] # proof that membership testing in Sets is fundamentally faster than Lists # the conservative value of 20 is to avoid false failures due to multitasking # > 250 is more typical # same thing, but 'not in' x += 1 start = DateTime.now for n in reps assert x not in s assert x not in s assert x not in s assert x not in s assert x not in s setTest = DateTime.now.subtract(start).totalSeconds start = DateTime.now for n in reps assert x not in t assert x not in t assert x not in t assert x not in t assert x not in t listTest = DateTime.now.subtract(start).totalSeconds # trace setTest, listTest ratio = listTest / setTest # 274.807564698076 # trace ratio assert ratio > 15, [listTest, setTest] # proof that membership testing in Sets is fundamentally faster than Lists # the conservative value of 15 is to avoid false failures due to multitasking