test-vec.py 1.39 KB
Newer Older
1 2
import alta

3
fail = 0
4

5 6
# Init the vector using an input array
# Also test if the _getitem_ function works
7 8
x = alta.vec([1.0, 1.0, 1.0])
if x[0] != 1.0:
9
    fail += 1
10

11 12 13 14 15
# Check if _setitem_ function works
x[0] = 0.0
if x[0] != 0.0:
    fail += 1

16 17 18 19
# Vector arithmetic
y = x + x
z = x - x
if y[0] != 2.0 and z[0] != 0.0:
20
    fail += 1
21

22
# Testing str(vec)
23
print("Testing 'str' method on variable 'y': " + str(y))
24

25 26
# Testing the length of the vector
if len(y) != 3:
27 28
    fail += 1

29
# Conversion to a list
30 31 32 33 34 35
l = list(y)
if len(l) != len(y):
    fail += 1
for i in range(0,len(y)):
    if l[i] != y[i]:
        fail += 1
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

# Print the test result
if fail > 0:
    print("Testing python 'vec' interface failed!")
    exit(1)
else:
    print("Testing python 'vec' interface passed!")
    exit(0)


###########################################################
##                                                       ##
## SPECIAL TEST: Interoperability with the numpy library ##
##                                                       ##
###########################################################

fail = 0

import numpy

57 58 59 60 61 62
# Conversion to a numpy array
na = numpy.array(y)
for i in range(0,len(y)):
    if na[i] != y[i]:
        fail += 1

63
# Print the test result
64
if fail > 0:
65
    print("Testing python 'vec' interface with 'numpy' failed!")
66 67
    exit(1)
else:
68
    print("Testing python 'vec' interface with 'numpy' passed!")
69
    exit(0)