xboa
TestTools.py
Go to the documentation of this file.
1 import os
2 
3 import StringIO
4 import sys
5 import time
6 import math
7 import string
8 try:
9  import numpy
10  from numpy import linalg
11 except ImportError:
12  pass
13 import operator
14 import bisect
15 
16 import xboa.Common as Common
17 
18 __float_tol = Common.float_tolerance
19 
20 def run_test_group(test_results, list_of_functions, list_of_args, alignment=40):
21  for i in range(len(list_of_functions)):
22  run_test(test_results, list_of_functions[i], list_of_args[i])
23 
24 def run_test(test_results, function, args, alignment=40):
25  test_out = None
26  test_name = function.__name__
27  test_name += ' '*(alignment - len(test_name))
28  try:
29  test_out = str(function(*args))
30  except Exception:
31  print 'Unhandled exception in test',function.__name__
32  sys.excepthook(*sys.exc_info())
33  test_out = 'fail'
34  except:
35  sys.excepthook(*sys.exc_info())
36  raise
37  test_results.append(test_name+test_out)
38 
39 def parse_tests(test_results, verbose_bool = True):
40  passes = 0
41  fails = 0
42  warns = 0
43  for line in test_results:
44  if verbose_bool: print line
45  if string.find(line, 'fail') >-1: fails +=1
46  elif string.find(line, 'False')>-1: fails +=1
47  elif string.find(line, 'pass') >-1: passes+=1
48  elif string.find(line, 'True') >-1: passes+=1
49  elif string.find(line, 'None') >-1: warns +=1
50  elif string.find(line, 'warn') < 0: warns +=1 #no output at all => warn
51 
52  if string.find(line, 'warn')>-1: warns +=1
53 
54  return (passes, fails, warns)
55 
56 
57 def test_root_hist(hist, name, x_label, y_label, xmin, xmax, ymin, ymax, line_color, line_style, line_width, fill_color, title):
58  minmax_hist = [hist.GetXaxis().GetXmin(), hist.GetXaxis().GetXmax(), hist.GetYaxis().GetXmin(), hist.GetYaxis().GetXmax()]
59  minmax_user = [xmin, xmax, ymin, ymax]
60  for i in range(4):
61  if abs(minmax_hist[i]-minmax_user[i])>Common.float_tolerance:
62  print 'test_root_hist failed on minmax',i,'with expected',minmax_user[i],'actual',minmax_hist[i]
63  return False
64 # print '@'+hist.GetXaxis().GetTitle()+'@','@'+x_label+'@',':','@'+hist.GetYaxis().GetTitle()+'@','@'+y_label+'@',':',hist.GetTitle(),title,':',hist.GetLineColor(),line_color,':'
65 # print hist.GetLineStyle(),line_style,':',hist.GetFillColor(),fill_color,':',hist.GetLineStyle(),line_style,':',hist.GetFillColor(),fill_color
66  testpass = hist.GetXaxis().GetTitle()==x_label and hist.GetYaxis().GetTitle()==y_label and hist.GetTitle()==title
67 # print testpass,hist.GetXaxis().GetTitle()==x_label,hist.GetYaxis().GetTitle()==y_label,hist.GetTitle()==title
68  testpass &= hist.GetLineColor()==line_color and hist.GetLineStyle()==line_style and hist.GetFillColor()==fill_color
69 # print testpass
70  testpass &= hist.GetLineStyle()==line_style and hist.GetFillColor()==fill_color
71 # print testpass
72  return testpass
73 
74 def test_root_canvas(canvas, name, title, bg_color, highlight_color, border_mode, frame_color):
75  testpass = True
76  testpass &= canvas.GetName().find(name) > -1
77  testpass &= canvas.GetTitle() == title
78  testpass &= canvas.GetFillColor() == bg_color
79  testpass &= canvas.GetBorderMode() == border_mode
80  testpass &= canvas.GetFrameFillColor() == frame_color
81  return testpass
82 
83 def test_root_canvas(canvas, name, title, bg_color, highlight_color, border_mode, frame_color):
84  testpass = True
85  testpass &= canvas.GetName().find(name) > -1
86  testpass &= canvas.GetTitle() == title
87  testpass &= canvas.GetFillColor() == bg_color
88  testpass &= canvas.GetBorderMode() == border_mode
89  testpass &= canvas.GetFrameFillColor() == frame_color
90  return testpass
91 
92 
93 def test_root_graph(graph, name_string, x_list, y_list, line_color, line_style, line_width, fill_color):
94  testpass = True
95  testpass &= graph.GetN()==len(x_list)
96  x = graph.GetX()
97  y = graph.GetY()
98  for i in range( len(x_list) ): testpass &= abs(x[i]-x_list[i])<__float_tol and abs(y[i]-y_list[i])<__float_tol
99 # for i in range( len(x_list) ): print x[i],x_list[i],':',y[i],y_list[i]
100  testpass &= graph.GetLineColor() == line_color
101  testpass &= graph.GetLineStyle() == line_style
102  testpass &= graph.GetLineWidth() == line_width
103  testpass &= graph.GetFillColor() == fill_color
104 # print graph.GetLineColor(),line_color,':',graph.GetLineStyle(),line_style,':',graph.GetLineWidth(),line_width,':',graph.GetFillColor(),fill_color,':'
105  return testpass
106