def __expect(machine, states): '''Quick test to compare addresses with expected stored values.''' from testing import __ok__ for state in states: if isinstance(states[state], str) and len(states[state]) > 1: data = machine.save(state, state+len(states[state])-1) else: data = machine.get(state) __ok__(data, states[state], '%s' % state)
def __testMemory(): '''Ensures that the memory in the machine works as expected.''' import testing ; from testing import __ok__ machine = Machine() machine.load('.' * Machine.RAMLEN) machine.prom('%' * Machine.PROMLEN) # ram writes should succeed for i in range(Machine.RAM, Machine.RAM+Machine.RAMLEN): machine.set(i, i) if machine.get(i) != i: __ok__(False) break # prom writes should fail silently for i in range(Machine.PROM, Machine.PROM+Machine.PROMLEN): machine.set(i, i) if machine.get(i) != ENCODING['%']: __ok__(False) break
def _circular_linear_test(): from testing import __ok__ c = Circular(V(), V.Y, 1) l = Linear(V.Y, V(2, -1, 0)) __ok__(str(c * l) == "Unity(V(1.0, 0.0, 0.0))") l = Linear(V.Y, -V.Y) __ok__(str(c * l) == "Nowhere()") l = Linear(V(0.5, 0, 0.5), V(0.5, 0, -0.5)) cl = c * l __ok__(isinstance(cl, Duality)) __ok__(cl.pointA[0] == cl.pointB[0])
def _circular_linear_test(): from testing import __ok__ c = Circular(V(), V.Y, 1) l = Linear(V.Y, V(2,-1,0)) __ok__(str(c*l) == "Unity(V(1.0, 0.0, 0.0))") l = Linear(V.Y, -V.Y) __ok__(str(c*l) == "Nowhere()") l = Linear(V(0.5, 0, 0.5), V(0.5, 0, -0.5)) cl = c*l __ok__(isinstance(cl, Duality)) __ok__(cl.pointA[0] == cl.pointB[0])
def _planar_planar_test(): from testing import __ok__ p = Planar(V(3, 0, -1), V(0, 3, 0)) q = Planar(V(5, 0, -5), V(1, 1, 0)) pq = p * q __ok__(isinstance(pq, Linear)) __ok__(equal(pq.pointB[1], pq.pointA[1])) q = Planar(V(5, 0, -5), V(0, 1, 0)) pq = p * q __ok__(pq is p) q = Planar(V(5, 1, -5), V(0, 1, 0)) pq = p * q __ok__(isinstance(pq, Nowhere))
def _planar_planar_test(): from testing import __ok__ p = Planar(V(3,0,-1), V(0,3,0)) q = Planar(V(5,0,-5), V(1,1,0)) pq = p*q __ok__(isinstance(pq, Linear)) __ok__(equal(pq.pointB[1], pq.pointA[1])) q = Planar(V(5,0,-5), V(0,1,0)) pq = p*q __ok__(pq is p) q = Planar(V(5,1,-5), V(0,1,0)) pq = p*q __ok__(isinstance(pq, Nowhere))
def __testStack(): '''Fill the stack with known values, and check it did the right thing.''' import testing ; from testing import __ok__ __ok__(Machine.STACK >= 9) machine = __run( ''' load A, "*" save A, [o%02o] load B, o10 load A, "A" :loop push A add A, o01 dec B jnz &loop stop ''' % (Machine.STACK-9) ) __expect(machine, { 'S':(Machine.STACK-8), (Machine.STACK-8):'HGFEDCBA', (Machine.STACK-9):ENCODING['*'] } ) if testing.__fails__(): machine.dump()
def __run(code, dump=False): '''Assemble and run code from scratch, returning the halted machine.''' from testing import __ok__ assy = Assembler() code = assy.assemble(code) __ok__(not assy.errors) machine = Machine() machine.reset() machine.speed = None if assy.errors: for error in assy.errors: print error else: if dump: for line in assy.listing: print line print repr(code) machine.prom(code) halted = machine.run(limit=5000) __ok__(halted) return machine
def _planar_linear_test(): from testing import __ok__ p = Planar(V(3, 0, -1), V(0, 3, 0)) __ok__(str(p) == "Planar(V(3.0, 0.0, -1.0), V(0.0, 1.0, 0.0))") l = Linear(V(2, 1, 1), V(0, -1, 1)) u = p * l __ok__(str(u) == "Unity(V(1.0, 0.0, 1.0))") p = Planar(V(3, -7, -1), V(1, 1, 0)) __ok__(equal(sqrt(2) / 2, p.normal[1]))
def _planar_linear_test(): from testing import __ok__ p = Planar(V(3,0,-1), V(0,3,0)) __ok__(str(p) == "Planar(V(3.0, 0.0, -1.0), V(0.0, 1.0, 0.0))") l = Linear(V(2,1,1), V(0,-1,1)) u = p*l __ok__(str(u) == "Unity(V(1.0, 0.0, 1.0))") p = Planar(V(3,-7,-1), V(1,1,0)) __ok__(equal(sqrt(2)/2, p.normal[1]))
def __test__(): import testing from testing import __ok__ __ok__(kana("ko n ni chi ha"), u'\u3053\u3093\u306b\u3061\u306f') __ok__(roma(u'\u3053\u3093\u3070\u3093\u306f'), "konbanha") __ok__(kana('kyokararyujubyo'), u'\u304d\u3087\u304b\u3089\u308a\u3085\u3058\u3045\u3073\u3087') __ok__(kana('kyo kara ryu ju byo'), kana('kyokararyujubyo')) __ok__(kana('tamagotchi'), u'\u305f\u307e\u3054\u3063\u3061') __ok__(kana('tamagotchi'), kana('tamagocchi')) __ok__(gyou('wa') == ('wa', 'wo')) __ok__(gyou('wa', rare=True) == ('wa', 'wi', 'we', 'wo')) __ok__(gyou('chi') == ('ta', 'chi', 'tsu', 'te', 'to')) __ok__(gyou('SHI') == ('SA', 'SHI', 'SU', 'SE', 'SO')) __ok__(normalize('a'), 'a') __ok__(normalize('tsu'), 'ta') __ok__(normalize('SHI'), 'SA') __ok__(normalize('FU'), 'HA') __ok__(normalize('ji'), 'za') __ok__(normalize('SHI'), 'SA') __ok__(normalize('pa'), 'pa') __ok__(normalize('n'), 'n') __ok__(normalize(kana('SHI')), kana('SA')) __ok__(tenten('gi'), True) __ok__(tenten('bi'), True) __ok__(tenten('zi'), True) __ok__(tenten('GI'), True) __ok__(tenten(kana('di')), True) __ok__(tenten('ta'), False) __ok__(tenten('PI'), False) __ok__(tenten(kana('chi')), False) __ok__(maru('PU'), True) __ok__(maru(kana('pi')), True) __ok__(maru('bu'), False) __ok__(maru(kana('ji')), False) for x in range(3): __ok__(choice('hi') in ['ha', 'hi', 'fu', 'he', 'ho']) __ok__(choice('MU') in ['MA', 'MI', 'MU', 'ME', 'MO']) __ok__( choice(u'\u3072') in [u'\u306f', u'\u3072', u'\u3075', u'\u3078', u'\u307b']) y = choice(family='HIRAGANA') __ok__(y, y.lower()) y = choice(family='katakana') __ok__(y, y.upper()) __ok__(kana('tonbo'), kana('tombo')) __ok__(kana("ton'bo"), kana("tonbo")) __ok__(roma(kana("tom'bo")), "tonbo") __ok__(roma(kana("shitsumon'")), "shitsumon") __ok__(html(u'Konnichiwa: \u3053\u3093\u306b\u3061\u306f'), 'Konnichiwa: こんにちは') testing.__report__()
def __test__(): import testing ; from testing import __ok__ __ok__(kana("ko n ni chi ha"), u'\u3053\u3093\u306b\u3061\u306f') __ok__(roma(u'\u3053\u3093\u3070\u3093\u306f'), "konbanha") __ok__(kana('kyokararyujubyo'), u'\u304d\u3087\u304b\u3089\u308a\u3085\u3058\u3045\u3073\u3087') __ok__(kana('kyo kara ryu ju byo'), kana('kyokararyujubyo')) __ok__(kana('tamagotchi'), u'\u305f\u307e\u3054\u3063\u3061') __ok__(kana('tamagotchi'), kana('tamagocchi')) __ok__(gyou('wa') == ('wa','wo')) __ok__(gyou('wa',rare=True) == ('wa','wi','we','wo')) __ok__(gyou('chi') == ('ta','chi','tsu','te','to')) __ok__(gyou('SHI') == ('SA','SHI','SU','SE','SO')) __ok__(normalize('a'), 'a') __ok__(normalize('tsu'), 'ta') __ok__(normalize('SHI'), 'SA') __ok__(normalize('FU'), 'HA') __ok__(normalize('ji'), 'za') __ok__(normalize('SHI'), 'SA') __ok__(normalize('pa'), 'pa') __ok__(normalize('n'), 'n') __ok__(normalize(kana('SHI')), kana('SA')) __ok__(tenten('gi'), True) __ok__(tenten('bi'), True) __ok__(tenten('zi'), True) __ok__(tenten('GI'), True) __ok__(tenten(kana('di')), True) __ok__(tenten('ta'), False) __ok__(tenten('PI'), False) __ok__(tenten(kana('chi')), False) __ok__(maru('PU'), True) __ok__(maru(kana('pi')), True) __ok__(maru('bu'), False) __ok__(maru(kana('ji')), False) for x in range(3): __ok__(choice('hi') in [ 'ha','hi','fu','he','ho' ]) __ok__(choice('MU') in [ 'MA','MI','MU','ME','MO' ]) __ok__(choice(u'\u3072') in [ u'\u306f', u'\u3072', u'\u3075', u'\u3078', u'\u307b' ] ) y = choice(family='HIRAGANA') __ok__(y, y.lower()) y = choice(family='katakana') __ok__(y, y.upper()) __ok__(kana('tonbo'), kana('tombo')) __ok__(kana("ton'bo"), kana("tonbo")) __ok__(roma(kana("tom'bo")), "tonbo") __ok__(roma(kana("shitsumon'")), "shitsumon") __ok__(html(u'Konnichiwa: \u3053\u3093\u306b\u3061\u306f'), 'Konnichiwa: こんにちは') testing.__report__()
def __test__(): from testing import __ok__, __report__ print 'Testing geometrical constraint classes...' # trivial constraints a1 = Anywhere() n1 = Nowhere() c1 = Circular(V.O, V.X, 1) __ok__(isinstance(a1 * a1, Anywhere)) __ok__(isinstance(a1 * n1, Nowhere)) __ok__(isinstance(n1 * n1, Nowhere)) __ok__(isinstance(a1 * c1, Circular)) __ok__((a1 * c1) is c1) __ok__(isinstance(n1 * c1, Nowhere)) # parametric constraints _linear_linear_test() _planar_linear_test() _planar_planar_test() _circular_linear_test() _circular_planar_test() _circular_circular_test() _spherical_linear_test() _spherical_planar_test() _spherical_circular_test() _spherical_spherical_test() __report__()
def _circular_circular_test(): from testing import __ok__ # tangent at origin c1 = Circular(V(-1, 0, 0), V.Y, 1) c2 = Circular(V(1, 0, 0), V.Y, 1) c1c2 = c1 * c2 __ok__(isinstance(c1c2, Unity)) __ok__(c1c2.point == V()) # cross off origin c1 = Circular(V.O, V.Y, 1) c2 = Circular(V.X, V.Y, 1) c1c2 = c1 * c2 __ok__(isinstance(c1c2, Duality)) __ok__(c1c2.pointA[0] == c1c2.pointB[0]) __ok__(equal(c1c2.pointA[1], -c1c2.pointB[1])) __ok__(equal(abs(c1c2.pointA[1]), 0.5 * sqrt(3)))
def _circular_planar_test(): from testing import __ok__ # coplanar c = Circular(V(-1, 0, 0), V.Y, 1) p = Planar(V(1, 0, 0), V.Y) cp = c * p __ok__(cp is c) # parallel planar c = Circular(V(-1, 0, 0), V.Y, 1) p = Planar(V(1, 1, 0), V.Y) cp = c * p __ok__(isinstance(cp, Nowhere)) # grab the tangent c = Circular(V(0, 0, 0), V.Y, 1) p = Planar(V(0, 1, 0), V(1, 1, 0)) cp = c * p __ok__(isinstance(cp, Unity)) __ok__(equal(cp.point[0], 1)) # grab a duality c = Circular(V(0, 0, 0), V.Y, 1) p = Planar(V(0, .5, 0), V(1, 1, 0)) cp = c * p __ok__(isinstance(cp, Duality)) __ok__(equal(abs(cp.pointA[2]), 0.5 * sqrt(3)))
def _circular_planar_test(): from testing import __ok__ # coplanar c = Circular(V(-1,0,0),V.Y,1) p = Planar(V( 1,0,0),V.Y) cp = c * p __ok__(cp is c) # parallel planar c = Circular(V(-1,0,0),V.Y,1) p = Planar(V( 1,1,0),V.Y) cp = c * p __ok__(isinstance(cp, Nowhere)) # grab the tangent c = Circular(V(0,0,0),V.Y,1) p = Planar(V(0,1,0),V(1,1,0)) cp = c * p __ok__(isinstance(cp, Unity)) __ok__(equal(cp.point[0], 1)) # grab a duality c = Circular(V(0,0,0),V.Y,1) p = Planar(V(0,.5,0),V(1,1,0)) cp = c * p __ok__(isinstance(cp, Duality)) __ok__(equal(abs(cp.pointA[2]), 0.5*sqrt(3)))
def _circular_circular_test(): from testing import __ok__ # tangent at origin c1 = Circular(V(-1,0,0),V.Y,1) c2 = Circular(V( 1,0,0),V.Y,1) c1c2 = c1 * c2 __ok__(isinstance(c1c2, Unity)) __ok__(c1c2.point == V()) # cross off origin c1 = Circular(V.O,V.Y,1) c2 = Circular(V.X,V.Y,1) c1c2 = c1 * c2 __ok__(isinstance(c1c2, Duality)) __ok__(c1c2.pointA[0] == c1c2.pointB[0]) __ok__(equal(c1c2.pointA[1], -c1c2.pointB[1])) __ok__(equal(abs(c1c2.pointA[1]), 0.5*sqrt(3)))
def __test__(): print 'Testing english module...' from testing import __ok__ __ok__(enumerate( [] ), '') __ok__(enumerate( ['a'] ), 'a') __ok__(enumerate( ['a','b'] ), 'a and b') __ok__(enumerate( ['a','b','c'] ), 'a, b and c') __ok__(enumerate( ['a','b','c','d'] ), 'a, b, c and d') __ok__(enumerate( [1,2,3,4] ), '1, 2, 3 and 4') __ok__(cardinal( -1234 ), 'negative one thousand, two hundred thirty-four') __ok__(cardinal( -1 ), 'negative one') __ok__(cardinal( 0 ), 'zero') __ok__(cardinal( 1 ), 'one') __ok__(cardinal( 8 ), 'eight') __ok__(cardinal( 18 ), 'eighteen') __ok__(cardinal( 28 ), 'twenty-eight') __ok__(cardinal( 280 ), 'two hundred eighty') __ok__(cardinal( 2800 ), 'two thousand, eight hundred') __ok__(cardinal( 280028 ), 'two hundred eighty thousand, twenty-eight') __ok__(cardinal( 2800000 ), 'two million, eight hundred thousand') __ok__(ordinal( -1234 ), 'negative one thousand, two hundred thirty-fourth') __ok__(ordinal( -1 ), 'negative first') __ok__(ordinal( 0 ), 'zeroth') __ok__(ordinal( 1 ), 'first') __ok__(ordinal( 8 ), 'eighth') __ok__(ordinal( 18 ), 'eighteenth') __ok__(ordinal( 28 ), 'twenty-eighth') __ok__(ordinal( 280 ), 'two hundred eightieth') __ok__(ordinal( 2800 ), 'two thousand, eight hundredth') __ok__(ordinal( 280028 ), 'two hundred eighty thousand, twenty-eighth') __ok__(ordinal( 2800000 ), 'two million, eight hundred thousandth') __ok__(conjugate('sleep', 'present'), 'sleeps') __ok__(conjugate('sleep', 'past'), 'slept') __ok__(conjugate('sleep', 'past participle'), 'slept') __ok__(conjugate('sleep', 'progressive'), 'sleeping') __ok__(conjugate('dream', 'past'), 'dreamt') __ok__(conjugate('rise', 'present'), 'rises') __ok__(conjugate('rise', 'past'), 'rose') __ok__(conjugate('rise', 'progressive'), 'rising') __ok__(conjugate('rise', 'past participle'), 'risen') __ok__(conjugate('chop', 'past'), 'chopped') __ok__(conjugate('chop', 'progressive'), 'chopping') __ok__(conjugate('quiz', 'progressive'), 'quizzing') __ok__(conjugate('meet', 'past'), 'met') __ok__(conjugate('meet', 'progressive'), 'meeting')
def __test__(): print 'Testing english module...' from testing import __ok__ __ok__(enumerate([]), '') __ok__(enumerate(['a']), 'a') __ok__(enumerate(['a', 'b']), 'a and b') __ok__(enumerate(['a', 'b', 'c']), 'a, b and c') __ok__(enumerate(['a', 'b', 'c', 'd']), 'a, b, c and d') __ok__(enumerate([1, 2, 3, 4]), '1, 2, 3 and 4') __ok__(cardinal(-1234), 'negative one thousand, two hundred thirty-four') __ok__(cardinal(-1), 'negative one') __ok__(cardinal(0), 'zero') __ok__(cardinal(1), 'one') __ok__(cardinal(8), 'eight') __ok__(cardinal(18), 'eighteen') __ok__(cardinal(28), 'twenty-eight') __ok__(cardinal(280), 'two hundred eighty') __ok__(cardinal(2800), 'two thousand, eight hundred') __ok__(cardinal(280028), 'two hundred eighty thousand, twenty-eight') __ok__(cardinal(2800000), 'two million, eight hundred thousand') __ok__(ordinal(-1234), 'negative one thousand, two hundred thirty-fourth') __ok__(ordinal(-1), 'negative first') __ok__(ordinal(0), 'zeroth') __ok__(ordinal(1), 'first') __ok__(ordinal(8), 'eighth') __ok__(ordinal(18), 'eighteenth') __ok__(ordinal(28), 'twenty-eighth') __ok__(ordinal(280), 'two hundred eightieth') __ok__(ordinal(2800), 'two thousand, eight hundredth') __ok__(ordinal(280028), 'two hundred eighty thousand, twenty-eighth') __ok__(ordinal(2800000), 'two million, eight hundred thousandth') __ok__(conjugate('sleep', 'present'), 'sleeps') __ok__(conjugate('sleep', 'past'), 'slept') __ok__(conjugate('sleep', 'past participle'), 'slept') __ok__(conjugate('sleep', 'progressive'), 'sleeping') __ok__(conjugate('dream', 'past'), 'dreamt') __ok__(conjugate('rise', 'present'), 'rises') __ok__(conjugate('rise', 'past'), 'rose') __ok__(conjugate('rise', 'progressive'), 'rising') __ok__(conjugate('rise', 'past participle'), 'risen') __ok__(conjugate('chop', 'past'), 'chopped') __ok__(conjugate('chop', 'progressive'), 'chopping') __ok__(conjugate('quiz', 'progressive'), 'quizzing') __ok__(conjugate('meet', 'past'), 'met') __ok__(conjugate('meet', 'progressive'), 'meeting')
def __test__(): from testing import __ok__, __report__ print('Testing geometrical constraint classes...') # trivial constraints a1 = Anywhere() n1 = Nowhere() c1 = Circular(V.O, V.X, 1) __ok__(isinstance(a1 * a1, Anywhere)) __ok__(isinstance(a1 * n1, Nowhere)) __ok__(isinstance(n1 * n1, Nowhere)) __ok__(isinstance(a1 * c1, Circular)) __ok__((a1 * c1) is c1) __ok__(isinstance(n1 * c1, Nowhere)) # parametric constraints _linear_linear_test() _planar_linear_test() _planar_planar_test() _circular_linear_test() _circular_planar_test() _circular_circular_test() _spherical_linear_test() _spherical_planar_test() _spherical_circular_test() _spherical_spherical_test() __report__()
def __test__(): from testing import __ok__ import timing print('Testing persistant object storage...') name = '_test_persist' if os.path.isdir(name): print('Directory %s already exists. Cannot complete tests.' % name) __ok__(name, None) return if os.path.exists(name + '.db'): print('Dbm file %s.db already exists. Cannot complete tests.' % name) __ok__(name, None) return count = 500 class _other (object): pass class X (Identified, Persistent, _other): pass for __ in ['_other','X']: globals()[__] = locals()[__] # use a filestore p = FileStore(name) __ok__(p, p) x = X() p.add(x) id = x.id() __ok__(id is not None) __ok__(p.exists(id), id in p) y = fetch(id) __ok__(x.id() == y.id()) __ok__(x is y) found = glob.glob('./%s/*' % name) __ok__(len(found), 1) __ok__(found[0], found[0]) y = x = None p.destroy(id) found = glob.glob('./%s/*' % name) __ok__(len(found), 0) y = fetch(id) __ok__(y, None) p.close() os.rmdir(name) print('Testing persistant filestore...') # use a filestore for many items p = FileStore(name) x = X() p.add(x) id = x.id() __ok__(id is not None) p.destroy(x) ids = set([]) timing.start() for n in range(1, count+1): y = X() y.number = n id = p.add(y) if n < 5: __ok__(id in ids, False) else: if id in ids: __ok__(False, 'id collision on #%d' % n) ids.add(id) timing.finish() print('%g created per second' % (float(n)/(timing.t1-timing.t0))) found = glob.glob('./%s/*.blob' % name) __ok__(len(found), len(ids)) p.close() p = FileStore(name) for id in ids: p.destroy(id) found = glob.glob('./%s/*/*' % name) __ok__(len(found), 0) p.close() os.rmdir(name) print('Testing persistent dbmstore...') # use a dbmstore p = DbmStore(name) x = X() p.add(x) id = x.id() __ok__(id is not None) __ok__(p.exists(id), id in p) y = fetch(id) __ok__(x.id() == y.id()) __ok__(x is y) y = x = None p.destroy(id) y = fetch(id) __ok__(y, None) ids = set([]) timing.start() for n in range(1, count+1): y = X() y.number = n id = p.add(y) if n < 5: __ok__(id in ids, False) else: if id in ids: __ok__(False, 'id collision on #%d' % n) ids.add(id) timing.finish() print('%g created per second' % (float(n)/(timing.t1-timing.t0))) p.close() p = DbmStore(name) for id in ids: p.destroy(id) p.close() os.unlink(name + '.db')
def __test__(): from testing import __ok__, __report__ import vectors ; from vectors import V,equal,zero print 'Testing interpolations...' __ok__( linear(0.5, 50, 60), 55 ) __ok__( linear(1, 2, 1.5, 50, 60), 55 ) __ok__( linear(1, 2, 1.5, V(50,500), V(60,600)), V(55,550) ) __ok__( bezier3(0.0, 0.0, 1.0, 0.0), 0.0 ) __ok__( bezier3(0.5, 0.0, 1.0, 0.0), 0.5 ) __ok__( equal( bezier3(0.2, 0.0, 1.0, 0.0), bezier3(0.8, 0.0, 1.0, 0.0) ) ) __ok__( bezier3(1.0, 0.0, 1.0, 0.0), 0.0 ) __ok__( bezier4(0.0, 0.0, 1.0, 1.0, 0.0), 0.0 ) __ok__( bezier4(0.5, 0.0, 1.0, 1.0, 0.0), 0.75 ) __ok__( equal( bezier4(0.2, 0.0, 1.0, 1.0, 0.0), bezier4(0.8, 0.0, 1.0, 1.0, 0.0) ) ) __ok__( bezier4(1.0, 0.0, 1.0, 1.0, 0.0), 0.0 ) __ok__( bezier(0.0, 0.0, 1.0, 1.0, 1.0, 0.0), 0.0 ) __ok__( bezier(0.5, 0.0, 1.0, 1.0, 1.0, 0.0), 0.875 ) __ok__( equal( bezier(0.2, 0.0, 1.0, 1.0, 1.0, 0.0), bezier(0.8, 0.0, 1.0, 1.0, 1.0, 0.0) ) ) __ok__( bezier(1.0, 0.0, 1.0, 1.0, 1.0, 0.0), 0.0 ) __ok__( bezier(1.0, 0.0, 1.0, 1.0, 1.0, 0.0), 0.0 ) __report__()
def __test__(): from testing import __ok__, __report__ print 'Testing basic math...' __ok__(equal(1.0, 1.0), True) __ok__(equal(1.0, 1.01), False) __ok__(equal(1.0, 1.0001), False) __ok__(equal(1.0, 0.9999), False) __ok__(equal(1.0, 1.0000001), False) __ok__(equal(1.0, 0.9999999), False) __ok__(equal(1.0, 1.0000000001), True) __ok__(equal(1.0, 0.9999999999), True) __ok__(equal(degrees(0), 0.0)) __ok__(equal(degrees(math.pi / 2), 90.0)) __ok__(equal(degrees(math.pi), 180.0)) __ok__(equal(radians(0.0), 0.0)) __ok__(equal(radians(90.0), math.pi / 2)) __ok__(equal(radians(180.0), math.pi)) print 'Testing V vector class...' # structural construction __ok__(V.O is not None, True) __ok__(V.O._v is not None, True) __ok__(V.O._v, (0., 0., 0.)) __ok__(V.O._l, 0.) __ok__(V.X._v, (1., 0., 0.)) __ok__(V.X._l, 1.) __ok__(V.Y._v, (0., 1., 0.)) __ok__(V.Y._l, 1.) __ok__(V.Z._v, (0., 0., 1.)) __ok__(V.Z._l, 1.) a = V(3., 2., 1.) __ok__(a._v, [3., 2., 1.]) a = V((1., 2., 3.)) __ok__(a._v, [1., 2., 3.]) a = V([1., 1., 1.]) __ok__(a._v, [1., 1., 1.]) a = V(0.) __ok__(a._v, [0.]) __ok__(a._l, 0.) a = V(3.) __ok__(a._v, [3.]) __ok__(a._l, 3.) # constants and direct comparisons __ok__(V.O, V(0., 0., 0.)) __ok__(V.X, V(1., 0., 0.)) __ok__(V.Y, V(0., 1., 0.)) __ok__(V.Z, V(0., 0., 1.)) # formatting and elements __ok__(repr(V.X), 'V(1.0, 0.0, 0.0)') __ok__(V.X[0], 1.) __ok__(V.X[1], 0.) __ok__(V.X[2], 0.) # simple addition __ok__(V.X + V.Y, V(1., 1., 0.)) __ok__(V.Y + V.Z, V(0., 1., 1.)) __ok__(V.X + V.Z, V(1., 0., 1.)) # didn't overwrite our constants, did we? __ok__(V.X, V(1., 0., 0.)) __ok__(V.Y, V(0., 1., 0.)) __ok__(V.Z, V(0., 0., 1.)) a = V(3., 2., 1.) b = a.normalize() __ok__(a != b) __ok__(a == V(3., 2., 1.)) __ok__(b.magnitude(), 1) b = a.magnitude(5) __ok__(a == V(3., 2., 1.)) __ok__(b.magnitude(), 5) __ok__(equal(b.dsquared(V.O), 25)) a = V(3., 2., 1.).normalize() __ok__(equal(a[0], 0.80178372573727319)) b = V(1., 3., 2.).normalize() __ok__(equal(b[2], 0.53452248382484879)) d = a.dot(b) __ok__(equal(d, 0.785714285714), True) __ok__(V(2., 2., 1.) * 3, V(6, 6, 3)) __ok__(3 * V(2., 2., 1.), V(6, 6, 3)) __ok__(V(2., 2., 1.) / 2, V(1, 1, 0.5)) v = V(1, 2, 3) w = V(4, 5, 6) __ok__(v.cross(w), V(-3, 6, -3)) __ok__(v.cross(w), v * w) __ok__(v * w, -(w * v)) __ok__(v.dot(w), 32) __ok__(v.dot(w), w.dot(v)) __ok__(zero(angle(V(1, 1, 1), V(2, 2, 2))), True) __ok__(equal(90.0, degrees(angle(V(1, 0, 0), V(0, 1, 0)))), True) __ok__(equal(180.0, degrees(angle(V(1, 0, 0), V(-1, 0, 0)))), True) __ok__(equal(0.0, degrees(track(V(1, 0)))), True) __ok__(equal(90.0, degrees(track(V(0, 1)))), True) __ok__(equal(180.0, degrees(track(V(-1, 0)))), True) __ok__(equal(270.0, degrees(track(V(0, -1)))), True) __ok__(equal(45.0, degrees(track(V(1, 1)))), True) __ok__(equal(135.0, degrees(track(V(-1, 1)))), True) __ok__(equal(225.0, degrees(track(V(-1, -1)))), True) __ok__(equal(315.0, degrees(track(V(1, -1)))), True) print 'Testing C complex number class...' __ok__(C(1, 2) is not None, True) __ok__(C(1, 2)[0], 1.0) __ok__(C(1 + 2j)[0], 1.0) __ok__(C((1, 2))[1], 2.0) __ok__(C(V([1, 2]))[1], 2.0) __ok__(C(3 + 2j) * C(1 + 4j), C(-5 + 14j)) try: __ok__(C(1, 2, 3) is not None, True) except TypeError: # takes exactly 2 elements __ok__(True, True) try: __ok__(C([1, 2, 3]) is not None, True) except TypeError: # takes exactly 2 elements __ok__(True, True) except TypeError: # takes exactly 2 elements __ok__(True, True) print 'Testing Q quaternion class...' __ok__(Q(1, 2, 3, 4) is not None, True) __ok__(Q(1, 2, 3, 4)[1], 2.0) __ok__(Q((1, 2, 3, 4))[2], 3.0) __ok__(Q(V(1, 2, 3, 4))[3], 4.0) __ok__(Q(), Q(0, 0, 0, 1)) __ok__(Q(1, 2, 3, 4).conjugate(), Q(-1, -2, -3, 4)) print 'Testing M matrix class...' m = M() __ok__(V(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), m) __ok__(m.row(0), V(1, 0, 0, 0)) __ok__(m.row(2), V(0, 0, 1, 0)) __ok__(m.col(1), V(0, 1, 0, 0)) __ok__(m.col(3), V(0, 0, 0, 1)) __ok__(m[5], 1.0) __ok__(m[1, 1], 1.0) __ok__(m[6], 0.0) __ok__(m[1, 2], 0.0) __ok__(m * V(1, 2, 3, 4), V(1, 2, 3, 4)) __ok__(V(1, 2, 3, 4) * m, V(1, 2, 3, 4)) mm = m * m __ok__(mm.__class__, M) __ok__(mm, M.I) mm = m * 2 __ok__(mm.__class__, M) __ok__(mm, 2.0 * m) __ok__(mm[3, 3], 2) __ok__(mm[3, 2], 0) __ok__(M.rotate('X', radians(90)), M.twist(Q.rotate('X', radians(90)))) __ok__(M.twist(Q(0, 0, 0, 1)), M.I) __ok__(M.twist(Q(.5, 0, 0, 1)), M.twist(Q(.5, 0, 0, 1).normalize())) __ok__(V.O * M.translate(V(1, 2, 3)), V(1, 2, 3, 1)) __ok__((V.X + V.Y + V.Z) * M.translate(V(1, 2, 3)), V(2, 3, 4, 1)) # need some tests on m.determinant() m = M() m = m.translate(V(1, 2, 3)) __ok__(m.inverse(), M().translate(-V(1, 2, 3))) m = m.rotate('Y', radians(30)) __ok__(m * m.inverse(), M.I) __report__()
def _linear_linear_test(): from testing import __ok__ # simple intersect at origin, commutative checks u = V(-1, 0, 0) v = -u c1 = Linear(u, v) r = V(0, -1, 0) s = -r c2 = Linear(r, s) __ok__(str(c1 * c2) == "Unity(V(0.0, 0.0, 0.0))") __ok__(str(c2 * c1) == "Unity(V(0.0, 0.0, 0.0))") c1 = Linear(v, u) __ok__(str(c1 * c2) == "Unity(V(0.0, 0.0, 0.0))") __ok__(str(c2 * c1) == "Unity(V(0.0, 0.0, 0.0))") # intersect away from origin o = V(0.125, 0.25, 0.5) u = V(-2, 3, -4) v = -u u += o v += o c1 = Linear(u, v) r = V(-3, 8, 21) s = -r r += o s += o c2 = Linear(r, s) __ok__(str(c2 * c1) == str(Unity(o))) __ok__(str(c1 * c2) == str(Unity(o))) # non-intersecting u = V(-1, 0, 0) v = -u u += V(0.0, 0.0, 0.5) v += V(0.0, 0.0, 0.5) c1 = Linear(u, v) r = V(0, -1, 0) s = -r c2 = Linear(r, s) __ok__(str(c1 * c2) == "Nowhere()") # intersecting r += 0.5 s += 0.5 c2 = Linear(r, s) __ok__(str(c1 * c2) == "Unity(V(0.5, 0.0, 0.5))") # colinear u = s + V(0, 0.5, 0) v = r + V(0, 0.5, 0) c1 = Linear(u, v) __ok__(str(c1 * c2) == str(c1))
def __test__(): from testing import __ok__, __report__ print('Testing basic math...') __ok__(equal(1.0, 1.0), True) __ok__(equal(1.0, 1.01), False) __ok__(equal(1.0, 1.0001), False) __ok__(equal(1.0, 0.9999), False) __ok__(equal(1.0, 1.0000001), False) __ok__(equal(1.0, 0.9999999), False) __ok__(equal(1.0, 1.0000000001), True) __ok__(equal(1.0, 0.9999999999), True) __ok__(equal(degrees(0), 0.0)) __ok__(equal(degrees(math.pi/2), 90.0)) __ok__(equal(degrees(math.pi), 180.0)) __ok__(equal(radians(0.0), 0.0)) __ok__(equal(radians(90.0), math.pi/2)) __ok__(equal(radians(180.0), math.pi)) print('Testing V vector class...') # structural construction __ok__(V.O is not None, True) __ok__(V.O._v is not None, True) __ok__(V.O._v, (0., 0., 0.)) ; __ok__(V.O._l, 0.) __ok__(V.X._v, (1., 0., 0.)) ; __ok__(V.X._l, 1.) __ok__(V.Y._v, (0., 1., 0.)) ; __ok__(V.Y._l, 1.) __ok__(V.Z._v, (0., 0., 1.)) ; __ok__(V.Z._l, 1.) a = V(3., 2., 1.) ; __ok__(a._v, [3., 2., 1.]) a = V((1., 2., 3.)) ; __ok__(a._v, [1., 2., 3.]) a = V([1., 1., 1.]) ; __ok__(a._v, [1., 1., 1.]) a = V(0.) ; __ok__(a._v, [0.]) ; __ok__(a._l, 0.) a = V(3.) ; __ok__(a._v, [3.]) ; __ok__(a._l, 3.) # constants and direct comparisons __ok__(V.O, V(0.,0.,0.)) __ok__(V.X, V(1.,0.,0.)) __ok__(V.Y, V(0.,1.,0.)) __ok__(V.Z, V(0.,0.,1.)) # formatting and elements __ok__(repr(V.X), 'V(1.0, 0.0, 0.0)') __ok__(V.X[0], 1.) __ok__(V.X[1], 0.) __ok__(V.X[2], 0.) # simple addition __ok__(V.X + V.Y, V(1.,1.,0.)) __ok__(V.Y + V.Z, V(0.,1.,1.)) __ok__(V.X + V.Z, V(1.,0.,1.)) # didn't overwrite our constants, did we? __ok__(V.X, V(1.,0.,0.)) __ok__(V.Y, V(0.,1.,0.)) __ok__(V.Z, V(0.,0.,1.)) a = V(3.,2.,1.) b = a.normalize() __ok__(a != b) __ok__(a == V(3.,2.,1.)) __ok__(b.magnitude(), 1) b = a.magnitude(5) __ok__(a == V(3.,2.,1.)) __ok__(b.magnitude(), 5) __ok__(equal(b.dsquared(V.O), 25)) a = V(3.,2.,1.).normalize() __ok__(equal(a[0], 0.80178372573727319)) b = V(1.,3.,2.).normalize() __ok__(equal(b[2], 0.53452248382484879)) d = a.dot(b) __ok__(equal(d, 0.785714285714), True) __ok__(V(2., 2., 1.) * 3, V(6, 6, 3)) __ok__(3 * V(2., 2., 1.), V(6, 6, 3)) __ok__(V(2., 2., 1.) / 2, V(1, 1, 0.5)) v = V(1,2,3) w = V(4,5,6) __ok__(v.cross(w), V(-3,6,-3)) __ok__(v.cross(w), v*w) __ok__(v*w, -(w*v)) __ok__(v.dot(w), 32) __ok__(v.dot(w), w.dot(v)) __ok__(zero(angle(V(1,1,1), V(2,2,2))), True) __ok__(equal(90.0, degrees(angle(V(1,0,0), V(0,1,0)))), True) __ok__(equal(180.0, degrees(angle(V(1,0,0), V(-1,0,0)))), True) __ok__(equal( 0.0, degrees(track(V( 1, 0)))), True) __ok__(equal( 90.0, degrees(track(V( 0, 1)))), True) __ok__(equal(180.0, degrees(track(V(-1, 0)))), True) __ok__(equal(270.0, degrees(track(V( 0,-1)))), True) __ok__(equal( 45.0, degrees(track(V( 1, 1)))), True) __ok__(equal(135.0, degrees(track(V(-1, 1)))), True) __ok__(equal(225.0, degrees(track(V(-1,-1)))), True) __ok__(equal(315.0, degrees(track(V( 1,-1)))), True) print('Testing C complex number class...') __ok__(C(1,2) is not None, True) __ok__(C(1,2)[0], 1.0) __ok__(C(1+2j)[0], 1.0) __ok__(C((1,2))[1], 2.0) __ok__(C(V([1,2]))[1], 2.0) __ok__(C(3+2j) * C(1+4j), C(-5+14j)) try: __ok__(C(1,2,3) is not None, True) except TypeError: # takes exactly 2 elements __ok__(True, True) try: __ok__(C([1,2,3]) is not None, True) except TypeError: # takes exactly 2 elements __ok__(True, True) except TypeError: # takes exactly 2 elements __ok__(True, True) print('Testing Q quaternion class...') __ok__(Q(1,2,3,4) is not None, True) __ok__(Q(1,2,3,4)[1], 2.0) __ok__(Q((1,2,3,4))[2], 3.0) __ok__(Q(V(1,2,3,4))[3], 4.0) __ok__(Q(), Q(0,0,0,1)) __ok__(Q(1,2,3,4).conjugate(), Q(-1,-2,-3,4)) print('Testing M matrix class...') m = M() __ok__(V(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1), m) __ok__(m.row(0), V(1,0,0,0)) __ok__(m.row(2), V(0,0,1,0)) __ok__(m.col(1), V(0,1,0,0)) __ok__(m.col(3), V(0,0,0,1)) __ok__(m[5], 1.0) __ok__(m[1,1], 1.0) __ok__(m[6], 0.0) __ok__(m[1,2], 0.0) __ok__(m * V(1,2,3,4), V(1,2,3,4)) __ok__(V(1,2,3,4) * m, V(1,2,3,4)) mm = m * m __ok__(mm.__class__, M) __ok__(mm, M.I) mm = m * 2 __ok__(mm.__class__, M) __ok__(mm, 2.0 * m) __ok__(mm[3,3], 2) __ok__(mm[3,2], 0) __ok__(M.rotate('X',radians(90)), M.twist(Q.rotate('X',radians(90)))) __ok__(M.twist(Q(0,0,0,1)), M.I) __ok__(M.twist(Q(.5,0,0,1)), M.twist(Q(.5,0,0,1).normalize())) __ok__(V.O * M.translate(V(1,2,3)), V(1,2,3,1)) __ok__((V.X+V.Y+V.Z) * M.translate(V(1,2,3)), V(2,3,4,1)) # need some tests on m.determinant() m = M() m = m.translate(V(1,2,3)) __ok__(m.inverse(), M().translate(-V(1,2,3))) m = m.rotate('Y', radians(30)) __ok__(m * m.inverse(), M.I) __report__()
def _linear_linear_test(): from testing import __ok__ # simple intersect at origin, commutative checks u = V(-1, 0, 0) ; v = -u ; c1 = Linear(u, v) r = V(0, -1, 0) ; s = -r ; c2 = Linear(r, s) __ok__(str(c1 * c2) == "Unity(V(0.0, 0.0, 0.0))") __ok__(str(c2 * c1) == "Unity(V(0.0, 0.0, 0.0))") c1 = Linear(v, u) __ok__(str(c1 * c2) == "Unity(V(0.0, 0.0, 0.0))") __ok__(str(c2 * c1) == "Unity(V(0.0, 0.0, 0.0))") # intersect away from origin o = V(0.125, 0.25, 0.5) u = V(-2, 3, -4) ; v = -u ; u += o ; v += o ; c1 = Linear(u, v) r = V(-3, 8, 21) ; s = -r ; r += o ; s += o ; c2 = Linear(r, s) __ok__(str(c2 * c1) == str(Unity(o))) __ok__(str(c1 * c2) == str(Unity(o))) # non-intersecting u = V(-1, 0, 0) ; v = -u u += V(0.0, 0.0, 0.5) v += V(0.0, 0.0, 0.5) c1 = Linear(u, v) r = V(0, -1, 0) ; s = -r c2 = Linear(r, s) __ok__(str(c1 * c2) == "Nowhere()") # intersecting r += 0.5 s += 0.5 c2 = Linear(r, s) __ok__(str(c1 * c2) == "Unity(V(0.5, 0.0, 0.5))") # colinear u = s + V(0, 0.5, 0) v = r + V(0, 0.5, 0) c1 = Linear(u, v) __ok__(str(c1 * c2) == str(c1))