|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 DIANA-HEP |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import json |
| 18 | +import math |
| 19 | +import sys |
| 20 | +import unittest |
| 21 | +import urllib2 |
| 22 | + |
| 23 | +from histogrammar import * |
| 24 | +import histogrammar.version |
| 25 | + |
| 26 | +tolerance = 1e-12 |
| 27 | +util.relativeTolerance = tolerance |
| 28 | +util.absoluteTolerance = tolerance |
| 29 | + |
| 30 | +class TestSpec(unittest.TestCase): |
| 31 | + def compare(self, x, y, name): |
| 32 | + if Factory.fromJson(x) != Factory.fromJson(y): |
| 33 | + sys.stderr.write(" FAILED " + name + "\n") |
| 34 | + sys.stderr.write(" PYTHON | SPECIFICATION\n") |
| 35 | + left = json.dumps(x, sort_keys=True, indent=2) |
| 36 | + right = json.dumps(y, sort_keys=True, indent=2) |
| 37 | + for leftline, rightline in zip(left.split("\n"), right.split("\n")): |
| 38 | + if leftline != rightline: |
| 39 | + sys.stderr.write("{0:50s} > {1}\n".format(leftline, rightline)) |
| 40 | + else: |
| 41 | + sys.stderr.write("{0:50s} | {1}\n".format(leftline, rightline)) |
| 42 | + self.assertEqual(Factory.fromJson(x), Factory.fromJson(y)) |
| 43 | + |
| 44 | + def runTest(self): |
| 45 | + sys.stdout.write("Downloading expected results, generated by specification {0}...\n".format(histogrammar.version.specification)) |
| 46 | + try: |
| 47 | + testdata = json.load(urllib2.urlopen("http://histogrammar.org/test/{0}/test-data.json".format(histogrammar.version.specification))) |
| 48 | + except Exception as err: |
| 49 | + sys.stdout.write("could not download http://histogrammar.org/test/{0}/test-data.json\nbecause of {1}: {2}\n".format(histogrammar.version.specification, err.__class__.__name__, str(err))) |
| 50 | + return |
| 51 | + try: |
| 52 | + testresults = json.load(urllib2.urlopen("http://histogrammar.org/test/{0}/test-results.json".format(histogrammar.version.specification))) |
| 53 | + except Exception as err: |
| 54 | + sys.stdout.write("could not download http://histogrammar.org/test/{0}/test-results.jsonbecause of {1}: {2}\n\n".format(histogrammar.version.specification, err.__class__.__name__, str(err))) |
| 55 | + return |
| 56 | + |
| 57 | + for x in testdata: |
| 58 | + for k, v in x.items(): |
| 59 | + if k != "strings" and v in ("nan", "inf", "-inf"): |
| 60 | + x[k] = float(v) |
| 61 | + |
| 62 | + def stripNames(x): |
| 63 | + if hasattr(x, "quantity"): |
| 64 | + x.quantity.name = None |
| 65 | + elif hasattr(x, "quantityName"): |
| 66 | + x.quantityName = None |
| 67 | + for xi in x.children: |
| 68 | + stripNames(xi) |
| 69 | + |
| 70 | + for testresult in testresults: |
| 71 | + sys.stderr.write(testresult["expr"] + "\n") |
| 72 | + |
| 73 | + zero = testresult["zero-named"] |
| 74 | + one = testresult["one-named"] |
| 75 | + two = testresult["two-named"] |
| 76 | + |
| 77 | + h1 = eval(testresult["expr"]) |
| 78 | + h2 = eval(testresult["expr"]) |
| 79 | + |
| 80 | + self.compare(h1.toJson(), zero, "NAMED ZERO") |
| 81 | + self.compare((h1 + h1).toJson(), zero, "NAMED ZERO + ZERO") |
| 82 | + self.compare(h1.zero().toJson(), zero, "NAMED ZERO.zero()") |
| 83 | + |
| 84 | + for x in testdata: |
| 85 | + h1.fill(x) |
| 86 | + h2.fill(x) |
| 87 | + self.compare(h1.toJson(), one, "NAMED ONE") |
| 88 | + self.compare(h1.zero().toJson(), zero, "NAMED ONE.zero()") |
| 89 | + self.compare((h1 + h1.zero()).toJson(), one, "NAMED ONE + ZERO") |
| 90 | + self.compare((h1.zero() + h1).toJson(), one, "NAMED ZERO + ONE") |
| 91 | + |
| 92 | + self.compare((h1 + h2).toJson(), two, "NAMED TWO VIA PLUS") |
| 93 | + |
| 94 | + for x in testdata: |
| 95 | + h1.fill(x) |
| 96 | + self.compare(h1.toJson(), two, "NAMED TWO VIA FILL") |
| 97 | + |
| 98 | + zero = testresult["zero-anonymous"] |
| 99 | + one = testresult["one-anonymous"] |
| 100 | + two = testresult["two-anonymous"] |
| 101 | + |
| 102 | + h1 = eval(testresult["expr"]) |
| 103 | + stripNames(h1) |
| 104 | + h2 = eval(testresult["expr"]) |
| 105 | + stripNames(h2) |
| 106 | + |
| 107 | + self.compare(h1.toJson(), zero, "ANONYMOUS ZERO") |
| 108 | + self.compare((h1 + h1).toJson(), zero, "ANONYMOUS ZERO + ZERO") |
| 109 | + self.compare(h1.zero().toJson(), zero, "ANONYMOUS ZERO.zero()") |
| 110 | + |
| 111 | + for x in testdata: |
| 112 | + h1.fill(x) |
| 113 | + h2.fill(x) |
| 114 | + self.compare(h1.toJson(), one, "ANONYMOUS ONE") |
| 115 | + self.compare(h1.zero().toJson(), zero, "ANONYMOUS ONE.zero()") |
| 116 | + self.compare((h1 + h1.zero()).toJson(), one, "ANONYMOUS ONE + ZERO") |
| 117 | + self.compare((h1.zero() + h1).toJson(), one, "ANONYMOUS ZERO + ONE") |
| 118 | + |
| 119 | + self.compare((h1 + h2).toJson(), two, "ANONYMOUS TWO VIA PLUS") |
| 120 | + |
| 121 | + for x in testdata: |
| 122 | + h1.fill(x) |
| 123 | + self.compare(h1.toJson(), two, "ANONYMOUS TWO VIA FILL") |
0 commit comments