1 | #/urs/bin/env python |
---|

2 | """ |
---|

3 | test_utils.py |
---|

4 | |
---|

5 | utility functions for testing modules |
---|

6 | """ |
---|

7 | |
---|

8 | import types |
---|

9 | |
---|

10 | def _floatCompare(f1,f2,thres=0.00001): |
---|

11 | """ |
---|

12 | compairs two float numbers for equality using a threshold value |
---|

13 | """ |
---|

14 | if f1 == f2: return True |
---|

15 | size = (abs(f1) + abs(f2))/2 |
---|

16 | return abs((f1-f2)/size) < thres |
---|

17 | |
---|

18 | def _areNumericListsEqualIgnoringOrder(result,answer): |
---|

19 | """ |
---|

20 | tests if two lists are equal (converts all values to float before comparison) |
---|

21 | """ |
---|

22 | if (len(result) != len(answer)): |
---|

23 | return False |
---|

24 | |
---|

25 | for x in answer: |
---|

26 | x = float(x) |
---|

27 | found = False |
---|

28 | for y in result: |
---|

29 | y = float(y) |
---|

30 | if _floatCompare(x,y): |
---|

31 | found = True |
---|

32 | break |
---|

33 | |
---|

34 | if not found: |
---|

35 | return False |
---|

36 | |
---|

37 | return True |
---|

38 | |
---|

39 | def _areNumericListsEqual(result,answer): |
---|

40 | """ |
---|

41 | tests if two lists are equal (converts all values to float before comparison) |
---|

42 | """ |
---|

43 | if (len(result) != len(answer)): |
---|

44 | return False |
---|

45 | |
---|

46 | for i in range(len(result)): |
---|

47 | x = float(result[i]) |
---|

48 | y = float(answer[i]) |
---|

49 | |
---|

50 | if not _floatCompare(x,y): |
---|

51 | return False |
---|

52 | |
---|

53 | return True |
---|

54 | |
---|

55 | def _comparePublicAttribues(A, B): |
---|

56 | |
---|

57 | #check the attributes of the axis |
---|

58 | for key in dir(A): |
---|

59 | if key[0] == '_': #ignore private attributes |
---|

60 | continue |
---|

61 | attA = getattr(A,key) |
---|

62 | if type(attA) == types.MethodType: #ignore methods |
---|

63 | continue |
---|

64 | #print key, type(getattr(axisA,key)), getattr(axisA,key) |
---|

65 | if getattr(A, key) != getattr(B,key): #compair the public attributes |
---|

66 | #print "attribute:", key, "doesn't match ", \ |
---|

67 | # getattr(axisA, key), ",", getattr(axisB,key) |
---|

68 | return False |
---|

69 | |
---|

70 | return True |
---|