PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/quick-test/quick-test.red

http://github.com/dockimbel/Red
Unknown | 186 lines | 163 code | 23 blank | 0 comment | 0 complexity | 95f7ac4a49bdb99b0d621af635fa9f4b MD5 | raw file
  1Red [
  2	Title:   "Red simple testing framework"
  3	Author:  "Peter W A Wood"
  4	File: 	 %quick-test.red
  5	Version: "0.2.0"
  6	Rights:  "Copyright (C) 2012-2015 Peter W A Wood. All rights reserved."
  7	License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
  8]
  9
 10;; counters
 11qt-run-tests: 0 
 12qt-run-asserts: 0
 13qt-run-passes: 0
 14qt-run-failures: 0
 15qt-file-tests: 0 
 16qt-file-asserts: 0 
 17qt-file-passes: 0 
 18qt-file-failures: 0
 19
 20qt-file-name: none
 21
 22;; group switches
 23qt-group-name-not-printed: true
 24qt-group?: false
 25
 26_qt-init-group: func [] [
 27  qt-group-name-not-printed: true
 28  qt-group?: false
 29  qt-group-name: ""
 30]
 31
 32qt-init-run: func [] [
 33  qt-run-tests: 0 
 34  qt-run-asserts: 0
 35  qt-run-passes: 0
 36  qt-run-failures: 0
 37  _qt-init-group
 38]
 39
 40qt-init-file: func [] [
 41  qt-file-tests: 0 
 42  qt-file-asserts: 0 
 43  qt-file-passes: 0 
 44  qt-file-failures: 0
 45  _qt-init-group
 46]
 47
 48***start-run***: func[
 49    title [string!]
 50][
 51  qt-init-run
 52  qt-run-name: title
 53  prin "***Starting*** " 
 54  print title
 55]
 56
 57~~~start-file~~~: func [
 58  title [string!]
 59][
 60  qt-init-file
 61  prin "~~~started test~~~ "
 62  print title
 63  qt-file-name: title
 64  qt-group?: false
 65]
 66
 67===start-group===: func [
 68  title [string!]
 69][
 70  qt-group-name: title
 71  qt-group?: true
 72]
 73
 74--test--: func [
 75  title [string!]
 76][
 77  qt-test-name: title
 78  qt-file-tests: qt-file-tests + 1
 79]
 80
 81--assert: func [
 82  assertion [logic!]
 83][
 84
 85  qt-file-asserts: qt-file-asserts + 1
 86  
 87  either assertion [
 88     qt-file-passes: qt-file-passes + 1
 89  ][
 90    qt-file-failures: qt-file-failures + 1
 91    if qt-group? [  
 92      if qt-group-name-not-printed [
 93        prin "===group=== "
 94        print qt-group-name
 95        qt-group-name-not-printed: false
 96      ]
 97    ]
 98    prin "--test-- " 
 99    prin qt-test-name
100    print " FAILED**************"
101  ]
102]
103
104--assertf~=: func[
105  x           [float!]
106  y           [float!]
107  e           [float!]
108  /local
109    diff      [float!]
110    e1        [float!]
111    e2        [float!]
112][
113  ;; calculate tolerance to use
114  ;;    as e * max (1, x, y)
115  either x > 0.0 [
116    e1: x * e
117  ][
118    e1: -1.0 * x * e
119  ]
120  if e > e1 [e1: e]
121  either y > 0.0 [
122    e2: y * e
123  ][
124    e2: -1.0 * y * e
125  ]
126  if e1 > e2 [e2: e1]
127
128  ;; perform almost equal check
129  either x > y [
130    diff: x - y
131  ][
132    diff: y - x
133  ]
134  either diff > e2 [
135    --assert false
136  ][
137    --assert true
138  ]
139]
140 
141===end-group===: func [] [
142  _qt-init-group
143]
144
145qt-print-totals: func [
146  tests     [integer!]
147  asserts   [integer!]
148  passes    [integer!]
149  failures  [integer!]
150][
151  prin  "  Number of Tests Performed:      " 
152  print tests 
153  prin  "  Number of Assertions Performed: "
154  print asserts
155  prin  "  Number of Assertions Passed:    "
156  print passes
157  prin  "  Number of Assertions Failed:    "
158  print failures
159  if failures <> 0 [
160    print "****************TEST FAILURES****************"
161  ]
162]
163
164~~~end-file~~~: func [] [
165  print ["~~~finished test~~~ " qt-file-name]
166  qt-print-totals qt-file-tests qt-file-asserts qt-file-passes qt-file-failures
167  print ""
168  
169  ;; update run totals
170  qt-run-passes: qt-run-passes + qt-file-passes
171  qt-run-asserts: qt-run-asserts + qt-file-asserts
172  qt-run-failures: qt-run-failures + qt-file-failures
173  qt-run-tests: qt-run-tests + qt-file-tests
174]
175
176***end-run***: func [][
177  prin "***Finished*** "
178  print qt-run-name
179  qt-print-totals qt-run-tests
180                  qt-run-asserts
181                  qt-run-passes
182                  qt-run-failures
183]
184
185
186