[Git][NTPsec/ntpsec][wip-ntpq-peers-display] 3 commits: Added test for rescaleunit. Simplified rescaleunit. Spelling fix.

Ian Bruene gitlab at mg.gitlab.com
Fri Mar 31 19:54:29 UTC 2017


Ian Bruene pushed to branch wip-ntpq-peers-display at NTPsec / ntpsec


Commits:
b6f45cea by Ian Bruene at 2017-03-31T12:37:59-05:00
Added test for rescaleunit. Simplified rescaleunit. Spelling fix.

Spelling fix: parts-per-thousand (ppt) is now parts-per-kilo (ppk) so as
not to conflict with parts-per-trillion (ppt).

- - - - -
7604e48f by Ian Bruene at 2017-03-31T14:36:53-05:00
Added test for scaleforunit. Tweaked scaleforunit.

- - - - -
91b41925 by Ian Bruene at 2017-03-31T14:54:05-05:00
Added test for oomsbetweenunits.

- - - - -


2 changed files:

- pylib/util.py
- tests/pylib/test_util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -43,8 +43,8 @@ UNITS_SEC = ["ns", "us", "ms", "s", "ks"]
 UNIT_PPT = 0
 UNIT_PPB = 1
 UNIT_PPM = 2
-UNIT_PPT = 3
-UNITS_PPX = ["ppt", "ppb", "ppm", "ppt"]
+UNIT_PPK = 3
+UNITS_PPX = ["ppt", "ppb", "ppm", "ppk"]
 
 
 # Variables that have units
@@ -122,13 +122,10 @@ def filtcooker(data):
     return rendered
 
 
-def rescaleunit(f, ooms):
+def rescaleunit(f, units):
     "Rescale a number by enough orders of magnitude for N units"
-    multiplier = 10 ** ooms
-    if ooms > 0:  # Shift down the unit list
-        f /= multiplier
-    else:
-        f *= multiplier
+    multiplier = 10 ** (units * 3)
+    f *= multiplier
     return f
 
 
@@ -146,6 +143,7 @@ def scaleforunit(f):
     multiplier = 10 ** -oom  # Reciprocol because floating * more accurate
     unitsmoved = oom // 3
     f *= multiplier
+    f = round(f, 13)  # Min round to catch something like 191.20000000000002
     return (f, int(unitsmoved))
 
 


=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -21,5 +21,53 @@ class TestPylibUtilMethods(unittest.TestCase):
             "[0000:1111:2222:3333:4444:5555:6666:7777]:123"),
             ("0000:1111:2222:3333:4444:5555:6666:7777", ":123"))
 
+    def test_rescaleunit(self):
+        self.assertEqual(ntp.util.rescaleunit(1.0, 0),
+                         1.0)
+        self.assertEqual(ntp.util.rescaleunit(10.0, 1),
+                         10000.0)
+        self.assertEqual(ntp.util.rescaleunit(0.1, 1),
+                         100.0)
+        self.assertEqual(ntp.util.rescaleunit(10.0, -1),
+                         0.01)
+        self.assertEqual(ntp.util.rescaleunit(0.1, -1),
+                         0.0001)
+        self.assertEqual(ntp.util.rescaleunit(-100.0, 1),
+                         -100000.0)
+        self.assertEqual(ntp.util.rescaleunit(-.001, 1),
+                         -1.0)
+        self.assertEqual(ntp.util.rescaleunit(-100.0, -1),
+                         -0.1)
+        self.assertEqual(ntp.util.rescaleunit(-.001, -1),
+                         -0.000001)
+
+    def test_scaleforunit(self):
+        self.assertEqual(ntp.util.scaleforunit(1.0),
+                         (1.0, 0))
+        self.assertEqual(ntp.util.scaleforunit(999.2342),
+                         (999.2342, 0))
+        self.assertEqual(ntp.util.scaleforunit(5042.7),
+                         (5.0427, 1))
+        self.assertEqual(ntp.util.scaleforunit(0.1912),
+                         (191.2, -1))
+        self.assertEqual(ntp.util.scaleforunit(0.00000042),
+                         (420.0, -3))
+        self.assertEqual(ntp.util.scaleforunit(-1.0),
+                         (-1.0, 0))
+        self.assertEqual(ntp.util.scaleforunit(-999.2342),
+                         (-999.2342, 0))
+        self.assertEqual(ntp.util.scaleforunit(-5042.7),
+                         (-5.0427, 1))
+        self.assertEqual(ntp.util.scaleforunit(-0.1912),
+                         (-191.2, -1))
+        self.assertEqual(ntp.util.scaleforunit(-0.00000042),
+                         (-420.0, -3))
+
+    def test_oomsbetweenunits(self):
+        self.assertEqual(ntp.util.oomsbetweenunits(3, 2),
+                         3)
+        self.assertEqual(ntp.util.oomsbetweenunits(2, 3),
+                         3)
+
 if __name__ == '__main__':
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/ae9644413de2e3f70f8fc834a0d1618936b568f6...91b41925c4640233788b0a57b08c6956c379d93b
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170331/6458f0f0/attachment.html>


More information about the vc mailing list