|
| 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 | + |
| 22 | +from histogrammar import * |
| 23 | + |
| 24 | +tolerance = 1e-12 |
| 25 | +util.relativeTolerance = tolerance |
| 26 | +util.absoluteTolerance = tolerance |
| 27 | + |
| 28 | +class TestSpec(unittest.TestCase): |
| 29 | + def compare(self, x, y, name): |
| 30 | + if Factory.fromJson(x) != Factory.fromJson(y): |
| 31 | + sys.stderr.write(" FAILED " + name + "\n") |
| 32 | + sys.stderr.write(" PYTHON | SPECIFICATION\n") |
| 33 | + left = json.dumps(x, sort_keys=True, indent=2) |
| 34 | + right = json.dumps(y, sort_keys=True, indent=2) |
| 35 | + for leftline, rightline in zip(left.split("\n"), right.split("\n")): |
| 36 | + if leftline != rightline: |
| 37 | + sys.stderr.write("{0:50s} > {1}\n".format(leftline, rightline)) |
| 38 | + else: |
| 39 | + sys.stderr.write("{0:50s} | {1}\n".format(leftline, rightline)) |
| 40 | + self.assertEqual(Factory.fromJson(x), Factory.fromJson(y)) |
| 41 | + |
| 42 | + def runTest(self): |
| 43 | + testdata = json.load(open("../histogrammar-multilang/test-data.json")) |
| 44 | + for x in testdata: |
| 45 | + for k, v in x.items(): |
| 46 | + if k != "strings" and v in ("nan", "inf", "-inf"): |
| 47 | + x[k] = float(v) |
| 48 | + |
| 49 | + testresults = json.load(open("../histogrammar-multilang/test-results.json")) |
| 50 | + |
| 51 | + for testresult in testresults: |
| 52 | + sys.stderr.write(testresult["expr"] + "\n") |
| 53 | + |
| 54 | + zero = testresult["zero-named"] |
| 55 | + one = testresult["one-named"] |
| 56 | + two = testresult["two-named"] |
| 57 | + |
| 58 | + h1 = eval(testresult["expr"]) |
| 59 | + h2 = eval(testresult["expr"]) |
| 60 | + self.compare(h1.toJson(), zero, "NAMED ZERO") |
| 61 | + |
| 62 | + for x in testdata: |
| 63 | + h1.fill(x) |
| 64 | + h2.fill(x) |
| 65 | + self.compare(h1.toJson(), one, "NAMED ONE") |
| 66 | + |
| 67 | + self.compare((h1 + h2).toJson(), two, "NAMED TWO VIA PLUS") |
| 68 | + |
| 69 | + for x in testdata: |
| 70 | + h1.fill(x) |
| 71 | + self.compare(h1.toJson(), two, "NAMED TWO VIA FILL") |
0 commit comments