def _executeTests(test, a, b, res, **kwargs): """ performes tests on operators """ almost = kwargs.get('almost', False) real = kwargs.get('real', True) res.reverse() test('_a+_b', a + b, res.pop(), **kwargs) test('_a-_b', a - b, res.pop(), **kwargs) test('_a*_b', a * b, res.pop(), **kwargs) test('_a/_b', a / b, res.pop(), **kwargs) test('_a**_b', a**b, res.pop(), **kwargs) test('_a|_b', a | b, res.pop(), **kwargs) test('_a&_b', a & b, res.pop(), **kwargs) test('_a==_b', a == b, res.pop()) test('_a!=_b', a != b, res.pop()) test('_a<<_b', a << b, res.pop(), **kwargs) test('_a>>_b', a >> b, res.pop(), **kwargs) test('-_a', -a, res.pop(), **kwargs) test('abs(-_a)', abs(-a), res.pop(), **kwargs) test('abs1(-_a)', m.ABS1(-a), res.pop(), **kwargs) test('abssq(-_a)', m.ABSSQ(-a), res.pop(), **kwargs) rcount = 11 acount = 4 + rcount if almost: # some operations only make sence on floating point test('exp(_a)', m.EXP(a), res.pop(), **kwargs) test('log(_a)', m.LOG(a), res.pop(), **kwargs) test('sin(_a)', m.SIN(a), res.pop(), **kwargs) test('cos(_a)', m.COS(a), res.pop(), **kwargs) if real: # some operations are only defined for real numbers test('tan(_a)', m.TAN(a), res.pop(), **kwargs) test('asin(_a/10)', m.ASIN(a / 10), res.pop(), **kwargs) test('acos(_a/10)', m.ACOS(a / 10), res.pop(), **kwargs) test('atan(_a)', m.ATAN(a), res.pop(), **kwargs) test('atan2(_a,1)', m.ATAN2(a, 1), res.pop(), **kwargs) test('log2(_a)', m.LOG2(a), res.pop(), **kwargs) test('log10(_a)', m.LOG10(a), res.pop(), **kwargs) test('sind(_a)', m.SIND(a), res.pop(), **kwargs) test('cosd(_a)', m.COSD(a), res.pop(), **kwargs) test('tand(_a)', m.TAND(a), res.pop(), **kwargs) test('anint(_a/3)', m.ANINT(a / 3), res.pop(), **kwargs) else: # strip results of real operations res = res[:-rcount] else: # strip results of floating operations res = res[:-acount] if real: # some binary operations are only defined for real numbers test('_a mod _b', a % b, res.pop(), **kwargs) test('_a>_b', a > b, res.pop()) test('_a>=_b', a >= b, res.pop()) test('_a<_b', a < b, res.pop()) test('_a<=_b', a <= b, res.pop())
def tdiFunctions(self): m.dTRUE = m.__dict__['$TRUE'] m.dFALSE = m.__dict__['$FALSE'] from MDSplus import mdsExceptions as Exc """Test Exceptions""" self._doExceptionTest('abort()', Exc.TdiABORT) self._doExceptionTest('{,}', Exc.TdiSYNTAX) self._doExceptionTest('\033[[A', Exc.TdiBOMB) self._doExceptionTest('abs()', Exc.TdiMISS_ARG) self._doExceptionTest('abs("")', Exc.TdiINVDTYDSC) self._doExceptionTest('abs(1,2)', Exc.TdiEXTRA_ARG) self._doExceptionTest('"', Exc.TdiUNBALANCE) """Test $Missing/NoData/None""" self._doTdiTest('', None) """Test abs""" self._doThreeTest('abs(cmplx(3.0,4.0))', m.ABS(m.Complex64(3. + 4.j)), m.Float32(5.)) """Test abs1""" self._doThreeTest('abs1(cmplx(3.0,4.0))', m.ABS1(m.Complex64(3. + 4.j)), m.Float32(7.)) """Test abssq""" self._doThreeTest('abssq(cmplx(3.0,4.0))', m.ABSSQ(m.Complex64(3. + 4.j)), m.Float32(25.)) """Test accumulate""" self._doThreeTestArray('accumulate([1,2,3])', m.ACCUMULATE(m.makeArray([1, 2, 3])), m.Int32Array([1, 3, 6])) self._doThreeTestArray( 'accumulate([[1,3,5],[2,4,6]])', m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]])), m.Int32Array([[1, 4, 9], [11, 15, 21]])) self._doThreeTestArray( 'accumulate([[1,3,5],[2,4,6]],0)', m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]]), 0), m.Int32Array([[1, 4, 9], [2, 6, 12]])) #self._doThreeTestArray('accumulate([[1,3,5],[2,4,6]],1)',m.ACCUMULATE([[1,3,5],[2,4,6]],1),m.Int32Array([[1,3,5],[3,7,11]])) # tdi issue self._doUnaryArray( m.Data.execute('accumulate([[1,3,5],[2,4,6]],1)'), m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]]), 1).getData()) """Test achar""" self._doThreeTest('achar(88)', m.ACHAR(88), m.String('X')) """Test ADJUSTL""" self._doThreeTest('adjustl(" WORD")', m.ADJUSTL(" WORD"), m.String("WORD ")) """Test ADJUSTR""" self._doThreeTest('adjustr("WORD ")', m.ADJUSTR("WORD "), m.String(" WORD")) """Test AIMAG""" self._doThreeTest('AIMAG(CMPLX(2.0,3.0))', m.AIMAG(m.CMPLX(2., 3.)), m.Float32(3.0)) """Test AINT""" self._doThreeTest('aint(2.783)', m.AINT(2.783), m.Float32(2.0)) self._doThreeTest('aint(-2.783)', m.AINT(-2.783), m.Float32(-2.0)) """Test NE (operates on flattened array, i.e. first 3 values are compared)""" A, B = m.makeArray([1, 3, 5]), m.makeArray([[0, 3, 5], [0, 0, 0], [0, 4, 8]]) self._doThreeTestArray( '_A=[1,3,5],_B=[[0,3,5],[0,0,0],[0,4,8]],_A ne _B', m.NE(A, B), m.Uint8Array([1, 0, 0])) """Test NE (operates on flattened array, i.e. first 3 values are compared)""" self._doThreeTestArray('_A eq _B', m.EQ(A, B), m.Uint8Array([0, 1, 1])) """Test ALL and ANY""" self._doThreeTest('all([$TRUE,$FALSE,$TRUE])', m.ALL(m.makeArray([1, 0, 1])), m.Uint8(0)) self._doThreeTest('any([$TRUE,$FALSE,$TRUE])', m.ANY(m.makeArray([1, 0, 1])), m.Uint8(1)) A = 0 self._doThreeTest('_A=0,all(_A eq _B)', m.ALL(m.EQ(A, B)), False) self._doThreeTest('any(_A ne _B)', m.ANY(m.NE(A, B)), True) self._doThreeTestArray('all(_A ne _B,0)', m.ALL(m.NE(A, B), 0), m.Uint8Array([0, 0, 0])) self._doThreeTestArray('any(_A ne _B,0)', m.ANY(m.NE(A, B), 0), m.Uint8Array([1, 0, 1])) self._doThreeTestArray('all(_A eq _B,1)', m.ALL(m.EQ(A, B), 1), m.Uint8Array([1, 0, 0])) self._doThreeTestArray('any(_A ne _B,1)', m.ANY(m.NE(A, B), 1), m.Uint8Array([0, 1, 1])) """Test allocated""" self.assertEqual(m.DEALLOCATE('*') >= 2, True) # deallocates _A and _B and more? self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(0)) self._doTdiTest('_xyz=0,allocated("_xyz")', m.Uint8(1)) self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(1)) self.assertEqual(m.DEALLOCATE('*'), m.Uint8(1)) self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(0)) """Test AND""" A, B = m.makeArray([0, 0, 1, 1]), m.makeArray([0, 1, 0, 1]) self._doThreeTestArray('_A=[0,0,1,1],_B=[0,1,0,1],_A && _B', m.AND(A, B), m.Uint8Array([0, 0, 0, 1])) """Test AND_NOT""" self._doThreeTestArray('_A AND_NOT _B', m.AND_NOT(A, B), m.Uint8Array([0, 0, 1, 0])) """Test ANINT""" self._doThreeTest('ANINT(2.783)', m.ANINT(2.783), m.Float32(3.0)) """Test ARG""" self._doTdiTest( 'execute("abs(arg(cmplx(3.0,4.0)) - .9272952) < .000001")', m.Uint8(1)) """Test ARGD""" self._doTdiTest( 'execute("abs(argd(cmplx(3.0,4.0)) - 53.1301) < .000001")', m.Uint8(1)) """Test arg_of""" self._doThreeTest('arg_of(pub->foo(42,43))', m.ARG_OF(m.Call('pub', 'foo', 42, 43)), m.Int32(42)) self._doThreeTest('arg_of(pub->foo(42,43),1)', m.ARG_OF(m.Call('pub', 'foo', 42, 43), 1), m.Int32(43)) self._doThreeTest('arg_of(1+3,1)', m.ARG_OF(m.ADD(1, 3), 1), m.Int32(3)) """Test Array""" self._doThreeTestArray('array(10)', m.ARRAY(10), m.Float32Array([0] * 10)) self._doThreeTestArray('array(10,0)', m.ARRAY(10, 0), m.Int32Array([0] * 10)) self._doThreeTestArray('array(10,0BU)', m.ARRAY(10, m.Uint8(0)), m.Uint8Array([0] * 10)) self._doThreeTestArray('zero(100)', m.ZERO(100), m.Float32Array([0] * 100))