[Git][NTPsec/ntpsec][pyutils-fixes] 4 commits: jupiter: Convert implicit casts to explicit.
Eric S. Raymond
gitlab at mg.gitlab.com
Tue Apr 4 03:58:34 UTC 2017
Eric S. Raymond pushed to branch pyutils-fixes at NTPsec / ntpsec
Commits:
46a83e8a by Gary E. Miller at 2017-04-03T14:47:29-07:00
jupiter: Convert implicit casts to explicit.
- - - - -
d98bfea7 by Gary E. Miller at 2017-04-03T16:15:17-07:00
oncore: Change implicit casts to explicit.
- - - - -
db33765f by Gary E. Miller at 2017-04-03T17:36:43-07:00
tests: pylib tests now get run.
Their output still does not get into test.log...
- - - - -
5b70d40d by Ian Bruene at 2017-04-04T03:57:55+00:00
Fixed tests and functions where appropriate.
- - - - -
6 changed files:
- ntpd/refclock_jupiter.c
- ntpd/refclock_oncore.c
- pylib/util.py
- tests/pylib/test_statfiles.py
- tests/pylib/test_util.py
- tests/wscript
Changes:
=====================================
ntpd/refclock_jupiter.c
=====================================
--- a/ntpd/refclock_jupiter.c
+++ b/ntpd/refclock_jupiter.c
@@ -766,8 +766,8 @@ jupiter_receive(struct recvbuf *rbufp)
bpcnt = rbufp->recv_length;
/* This shouldn't happen */
- if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
- bpcnt = sizeof(instance->sbuf) - instance->ssize;
+ if (bpcnt > sizeof(instance->sbuf) - (size_t)instance->ssize)
+ bpcnt = sizeof(instance->sbuf) - (size_t)instance->ssize;
/* Append to input buffer */
memcpy((uint8_t *)instance->sbuf + instance->ssize, bp, bpcnt);
@@ -780,7 +780,7 @@ jupiter_receive(struct recvbuf *rbufp)
tstamp = rbufp->recv_time;
hp = (struct jheader *)instance->sbuf;
sp = (u_short *)(hp + 1);
- size = cc - sizeof(*hp);
+ size = cc - (int)sizeof(*hp);
switch (getshort(hp->id)) {
case JUPITER_O_PULSE:
@@ -803,7 +803,8 @@ jupiter_receive(struct recvbuf *rbufp)
* this one.
*/
laststime = instance->stime;
- instance->stime = DS2UI(((struct jpulse *)sp)->stime);
+ instance->stime =
+ (uint32_t)DS2UI(((struct jpulse *)sp)->stime);
if (laststime != 0 && instance->stime - laststime <= 21) {
jupiter_debug(peer, __func__,
"avoided firmware bug (stime %.2f, laststime %.2f)",
@@ -839,7 +840,7 @@ jupiter_receive(struct recvbuf *rbufp)
break;
/* Add the new sample to a median filter */
- tstamp = lfpinit(JAN_1970 + (uint32_t)last_timecode, 0);
+ tstamp = lfpinit(JAN_1970 + last_timecode, 0);
refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
@@ -948,7 +949,7 @@ jupiter_parse_t(struct instance *instance, u_short *sp)
jp = (struct jpulse *)sp;
/* The timecode is presented as seconds into the current GPS week */
- sweek = DS2UI(jp->sweek) % WEEKSECS;
+ sweek = (u_int)DS2UI(jp->sweek) % WEEKSECS;
/*
* If we don't know the current GPS week, calculate it from the
@@ -1080,7 +1081,7 @@ jupiter_parse_gpos(struct instance *instance, u_short *sp)
}
instance->gpos_gweek = jg->gweek;
- instance->gpos_sweek = DS2UI(jg->sweek);
+ instance->gpos_sweek = (u_int)DS2UI(jg->sweek);
/* coverity[tainted_data] */
while(instance->gpos_sweek >= WEEKSECS) {
instance->gpos_sweek -= WEEKSECS;
@@ -1321,7 +1322,7 @@ jupiter_recv(struct instance *instance)
return (0);
}
- if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
+ if (jupiter_cksum(sp, ((u_int)cc / sizeof(u_short) - 1U)) !=
getshort(hp->hsum)) {
jupiter_debug(instance->peer, __func__, "bad header checksum!");
/* This is drastic but checksum errors should be rare */
@@ -1332,14 +1333,14 @@ jupiter_recv(struct instance *instance)
/* Check for a payload */
len = getshort(hp->len);
if (len > 0) {
- n = (len + 1) * sizeof(u_short);
+ n = (len + 1) * (int)sizeof(u_short);
/* Not enough data yet */
if (size < cc + n)
return (0);
/* Check payload checksum */
sp = (u_short *)(hp + 1);
- if (jupiter_cksum(sp, len) != getshort(sp[len])) {
+ if (jupiter_cksum(sp, (u_int)len) != getshort(sp[len])) {
jupiter_debug(instance->peer,
__func__, "bad payload checksum!");
/* This is drastic but checksum errors should be rare */
=====================================
ntpd/refclock_oncore.c
=====================================
--- a/ntpd/refclock_oncore.c
+++ b/ntpd/refclock_oncore.c
@@ -558,7 +558,7 @@ static uint8_t oncore_cmd_Ia[] = { 'I', 'a' }; /* 12 Self Test */
/* to buffer, int w, uint8_t *buf */
#define w32_buf(buf,w) { u_int i_tmp; \
- i_tmp = (w<0) ? (~(-w)+1) : (w); \
+ i_tmp = (u_int)((w<0) ? (~(-w)+1) : (w)); \
(buf)[0] = (i_tmp >> 24) & 0xff; \
(buf)[1] = (i_tmp >> 16) & 0xff; \
(buf)[2] = (i_tmp >> 8) & 0xff; \
@@ -663,7 +663,7 @@ oncore_start(
}
if (stat(device2, &stat2)) {
- stat2.st_dev = stat2.st_ino = -2;
+ stat2.st_dev = stat2.st_ino = (unsigned long)-2;
oncore_log_f(instance, LOG_ERR, "Can't stat fd2 (%s) %d %m",
device2, errno);
}
@@ -1508,7 +1508,7 @@ oncore_consume(
continue;
}
- l = oncore_messages[m].len;
+ l = (unsigned int)oncore_messages[m].len;
#ifdef ONCORE_VERBOSE_CONSUME
if (debug > 3)
oncore_log_f(instance, LOG_DEBUG,
@@ -1529,13 +1529,14 @@ oncore_consume(
oncore_log(instance, LOG_DEBUG, "NO <CR><LF> at end of message");
#endif
} else { /* check the CheckSum */
- if (oncore_checksum_ok(rcvbuf, l)) {
+ if (oncore_checksum_ok(rcvbuf, (int)l)) {
if (instance->shmem != NULL) {
instance->shmem[oncore_messages[m].shmem + 2]++;
memcpy(instance->shmem + oncore_messages[m].shmem + 3,
rcvbuf, (size_t) l);
}
- oncore_msg_any(instance, rcvbuf, (size_t) (l-3), m);
+ oncore_msg_any(instance, rcvbuf,
+ (size_t)(l-3), (int)m);
if (oncore_messages[m].handler)
oncore_messages[m].handler(instance, rcvbuf, (size_t) (l-3));
}
@@ -2025,7 +2026,7 @@ oncore_msg_Ay(
instance->saw_Ay = 1;
- instance->offset = buf_w32(&buf[4]);
+ instance->offset = (u_long)buf_w32(&buf[4]);
oncore_log_f(instance, LOG_INFO, "PPS Offset is set to %ld ns",
instance->offset);
@@ -2051,7 +2052,7 @@ oncore_msg_Az(
instance->saw_Az = 1;
- instance->delay = buf_w32(&buf[4]);
+ instance->delay = (u_long)buf_w32(&buf[4]);
oncore_log_f(instance, LOG_INFO, "Cable delay is set to %ld ns",
instance->delay);
@@ -2095,7 +2096,7 @@ oncore_msg_BaEaHa(
instance->count3 = 0;
if (instance->chan_in != -1) /* set in Input */
- instance->chan = instance->chan_in;
+ instance->chan = (uint8_t)instance->chan_in;
else /* set from test */
instance->chan = instance->chan_ck;
@@ -2750,8 +2751,9 @@ oncore_msg_Cb(
}
i *= 36;
- instance->shmem[instance->shmem_Cb + i + 2]++;
- memcpy(instance->shmem + instance->shmem_Cb + i + 3, buf, (size_t) (len + 3));
+ instance->shmem[(int)instance->shmem_Cb + i + 2]++;
+ memcpy(instance->shmem + instance->shmem_Cb + i + 3, buf,
+ (size_t)(len + 3));
#ifdef ONCORE_VERBOSE_MSG_CB
oncore_log_f(instance, LOG_DEBUG, "See Cb [%d,%d]", buf[4],
@@ -2883,8 +2885,8 @@ oncore_msg_Cj_id(
/* next, the Firmware Version and Revision numbers */
- instance->version = atoi((char *) &instance->Cj[83]);
- instance->revision = atoi((char *) &instance->Cj[111]);
+ instance->version = (u_int)atoi((char *) &instance->Cj[83]);
+ instance->revision = (u_int)atoi((char *) &instance->Cj[111]);
/* from model number decide which Oncore this is,
and then the number of channels */
@@ -3068,7 +3070,8 @@ oncore_msg_Cj_init(
oncore_sendmsg(instance, oncore_cmd_Ea0, sizeof(oncore_cmd_Ea0));
oncore_sendmsg(instance, oncore_cmd_En0, sizeof(oncore_cmd_En0));
oncore_sendmsg(instance, oncore_cmd_Ha, sizeof(oncore_cmd_Ha ));
- oncore_cmd_Gc[2] = (instance->pps_control < 0) ? 1 : instance->pps_control;
+ oncore_cmd_Gc[2] = (uint8_t)((instance->pps_control < 0) ?
+ 1 : instance->pps_control);
oncore_sendmsg(instance, oncore_cmd_Gc, sizeof(oncore_cmd_Gc)); /* PPS off/continuous/Tracking 1+sat/TRAIM */
}
@@ -3840,7 +3843,7 @@ oncore_set_traim(
if (instance->traim_in != -1) /* set in Input */
instance->traim = instance->traim_in;
else
- instance->traim = instance->traim_ck;
+ instance->traim = (int8_t)instance->traim_ck;
oncore_log_f(instance, LOG_INFO, "Input says TRAIM = %d",
instance->traim_in);
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -31,8 +31,6 @@ OLD_CTL_PST_SEL_SYNCCAND = 2
OLD_CTL_PST_SEL_SYSPEER = 3
-timefuzz = 1e-9 # Time variables have max precision of 1ns. Use for x==0.0.
-
# Units for formatting
UNIT_NS = 0
UNIT_US = 1
@@ -90,6 +88,11 @@ def portsplit(hostname):
return (hostname, portsuffix)
+def zerowiggle(ooms):
+ "Generate a wiggle value for float==0 comparisons"
+ return 10 ** -ooms
+
+
def filtcooker(data):
"Cooks the string of space seperated numbers with units"
parts = data.split()
@@ -99,7 +102,7 @@ def filtcooker(data):
for part in parts:
part = float(part)
floatyparts.append(part)
- value, oom = scaleforunit(part)
+ value, oom = scaleforunit(part, oomsbetweenunits(UNIT_MS, UNIT_NS))
oomcount[oom] = oomcount.get(oom, 0) + 1
# Find the most common unit
mostcommon = None
@@ -109,15 +112,16 @@ def filtcooker(data):
if count > highestcount:
mostcommon = key
highestcount = count
- newunit = mostcommon + UNIT_MS
+ newunit = UNITS_SEC[mostcommon + UNIT_MS]
+ oomstobase = (mostcommon * 3) + 6 # 6 == UNIT_MS distance from base
# Shift all values to the new unit
cooked = []
for part in floatyparts:
part = rescaleunit(part, mostcommon)
- fmt = formatdigitsplit(part, 7)
+ fmt = formatdigitsplit(part, 7, oomstobase)
temp = fmt % part
cooked.append(temp)
- rendered = " ".join(cooked) + " " + UNITS_SEC[newunit]
+ rendered = " ".join(cooked) + " " + newunit
return rendered
@@ -128,9 +132,11 @@ def rescaleunit(f, units):
return f
-def scaleforunit(f):
+def scaleforunit(f, oomstobase):
"Scales a number by units to keep it in the range 0.000-999.9"
- if -timefuzz < f < timefuzz: # if sufficiently close to zero do nothing
+ f = round(f, oomstobase) # pre-round to base unit to filter float folly
+ wiggle = zerowiggle(oomstobase)
+ if -wiggle < f < wiggle: # if sufficiently close to zero do nothing
return (f, 0)
unitsmoved = 0
af = abs(f)
@@ -140,29 +146,33 @@ def scaleforunit(f):
oom = math.log10(af) # Orders Of Magnitude
oom -= oom % 3 # We only want to move in groups of 3 ooms
multiplier = 10 ** -oom # Reciprocol because floating * more accurate
- unitsmoved = oom // 3
+ unitsmoved = int(oom // 3)
f *= multiplier
- f = round(f, 13) # Min round to catch something like 191.20000000000002
- return (f, int(unitsmoved))
+ roundooms = (unitsmoved * 3) + oomstobase
+ f = round(f, roundooms) # Filter out any float folly we introduced here
+ return (f, unitsmoved)
-def formatdigitsplit(f, fieldsize):
+def roundsubzero(f, oomstobase):
+ "Rounds a number at it's base unit"
+ mul = 10 ** oomstobase
+ f *= mul
+ f = round(f)
+ return f
+
+
+def formatdigitsplit(f, fieldsize, oomstobase):
"Create a format string for a float without adding fake precision."
af = abs(f)
- if f.is_integer() or (af < timefuzz):
- return "%" + str(fieldsize) + "d"
if af >= 100.0:
maxdigits = fieldsize - 4 # xxx.
elif af >= 10.0:
maxdigits = fieldsize - 3 # xx.
- elif af >= 1.0:
+ else:
maxdigits = fieldsize - 2 # x.
- else: # Yes, this can happen with filts
- maxdigits = fieldsize - 2 # 0.
if f < 0.0:
maxdigits -= 1 # need to fit a negative symbol
- sig = len(str(f).split(".")[1]) # count digits after the decimal point
- subdigits = min(sig, maxdigits) # use min so we don't add fake data
+ subdigits = min(oomstobase, maxdigits) # use min so we don't add fake data
formatter = "%" + str(fieldsize) + "." + str(subdigits) + "f"
return formatter
@@ -180,17 +190,19 @@ def unitformatter(f, unitgroup, startingunit, baseunit=None,
strip = True
if baseunit is None:
baseunit = 0 # Assume that the lowest unit is equal to LSB
- if -timefuzz < f < timefuzz: # Zero, don't show decimals
- unit = unitgroup[baseunit] # all the way to the lsb
- rendered = "0" + unit
+ oomsfrombase = oomsbetweenunits(startingunit, baseunit)
+ wiggle = zerowiggle(oomsfrombase)
+ if -wiggle < f < wiggle: # Zero, don't show decimals
+ unit = unitgroup[baseunit] # go all the way to the lsb
+ f = roundsubzero(f, oomsfrombase)
+ rendered = ("%d" % f) + unit
if not strip:
rendered = padder(rendered)
return rendered
oldf = f # keep this in case we don't fit in the units
- f, unitsmoved = scaleforunit(f)
+ f, unitsmoved = scaleforunit(f, oomsfrombase)
unitget = startingunit + unitsmoved
- rounddigits = oomsbetweenunits(unitget, baseunit)
- f = round(f, rounddigits)
+ oomsfrombase = oomsbetweenunits(unitget, baseunit) # will need this later
if (0 <= unitget < len(unitgroup)):
unit = unitgroup[unitget]
if width is None: # Don't care about size, just display everything
@@ -207,7 +219,7 @@ def unitformatter(f, unitgroup, startingunit, baseunit=None,
if unitget == baseunit: # Don't want fake decimals
formatter = "%" + str(displaysize) + "d"
else:
- formatter = formatdigitsplit(f, displaysize)
+ formatter = formatdigitsplit(f, displaysize, oomsfrombase)
rendered = (formatter % f) + unit
if strip:
rendered = rendered.strip()
=====================================
tests/pylib/test_statfiles.py
=====================================
--- a/tests/pylib/test_statfiles.py
+++ b/tests/pylib/test_statfiles.py
@@ -1,3 +1,6 @@
+#!/usr/bin/env python
+# coding: utf-8
+
import unittest
import ntp.statfiles
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -1,3 +1,6 @@
+#!/usr/bin/env python
+# coding: utf-8
+
import unittest
import ntp.util
@@ -44,26 +47,30 @@ class TestPylibUtilMethods(unittest.TestCase):
-0.000001)
def test_scaleforunit(self):
- self.assertEqual(ntp.util.scaleforunit(1.0),
+ self.assertEqual(ntp.util.scaleforunit(1.0, 9),
(1.0, 0))
- self.assertEqual(ntp.util.scaleforunit(999.2342),
+ self.assertEqual(ntp.util.scaleforunit(999.2342, 9),
(999.2342, 0))
- self.assertEqual(ntp.util.scaleforunit(5042.7),
+ self.assertEqual(ntp.util.scaleforunit(5042.7, 9),
(5.0427, 1))
- self.assertEqual(ntp.util.scaleforunit(0.1912),
+ self.assertEqual(ntp.util.scaleforunit(0.1912, 9),
(191.2, -1))
- self.assertEqual(ntp.util.scaleforunit(0.00000042),
+ self.assertEqual(ntp.util.scaleforunit(0.00000042, 9),
(420.0, -3))
- self.assertEqual(ntp.util.scaleforunit(-1.0),
+ self.assertEqual(ntp.util.scaleforunit(-1.0, 9),
(-1.0, 0))
- self.assertEqual(ntp.util.scaleforunit(-999.2342),
+ self.assertEqual(ntp.util.scaleforunit(-999.2342, 9),
(-999.2342, 0))
- self.assertEqual(ntp.util.scaleforunit(-5042.7),
+ self.assertEqual(ntp.util.scaleforunit(-5042.7, 9),
(-5.0427, 1))
- self.assertEqual(ntp.util.scaleforunit(-0.1912),
+ self.assertEqual(ntp.util.scaleforunit(-0.1912, 9),
(-191.2, -1))
- self.assertEqual(ntp.util.scaleforunit(-0.00000042),
+ self.assertEqual(ntp.util.scaleforunit(-0.00000042, 9),
(-420.0, -3))
+ self.assertEqual(ntp.util.scaleforunit(1.0000004, 6),
+ (1.0, 0))
+ self.assertEqual(ntp.util.scaleforunit(1.0000005, 6),
+ (1.000001, 0))
def test_oomsbetweenunits(self):
self.assertEqual(ntp.util.oomsbetweenunits(3, 2),
@@ -74,23 +81,23 @@ class TestPylibUtilMethods(unittest.TestCase):
def test_filtcooker(self):
self.assertEqual(ntp.util.filtcooker(
"1.02 34.5 0.67835 -23.0 9 6.7 1.0 .1"),
- " 1.02 34.5 0.67835 -23 9 6.7 1 0.1 ms"
+ "1.02000 34.5000 0.67835 -23.000 9.00000 6.70000 1.00000 0.10000 ms"
)
def test_formatdigitsplit(self):
- self.assertEqual(ntp.util.formatdigitsplit(10.0, 5),
- "%5d")
- self.assertEqual(ntp.util.formatdigitsplit(100.52, 5),
+ self.assertEqual(ntp.util.formatdigitsplit(10.0, 5, 9),
+ "%5.2f")
+ self.assertEqual(ntp.util.formatdigitsplit(100.52, 5, 9),
"%5.1f")
- self.assertEqual(ntp.util.formatdigitsplit(10.123456789, 8),
+ self.assertEqual(ntp.util.formatdigitsplit(10.123456789, 8, 9),
"%8.5f")
- self.assertEqual(ntp.util.formatdigitsplit(1.123456789, 8),
+ self.assertEqual(ntp.util.formatdigitsplit(1.123456789, 8, 9),
"%8.6f")
- self.assertEqual(ntp.util.formatdigitsplit(0.123456789, 8),
+ self.assertEqual(ntp.util.formatdigitsplit(0.123456789, 8, 9),
"%8.6f")
- self.assertEqual(ntp.util.formatdigitsplit(1.234, 10),
- "%10.3f")
- self.assertEqual(ntp.util.formatdigitsplit(-1.23456789, 6),
+ self.assertEqual(ntp.util.formatdigitsplit(1.234, 10, 9),
+ "%10.8f")
+ self.assertEqual(ntp.util.formatdigitsplit(-1.23456789, 6, 9),
"%6.3f")
def test_unitformatter(self):
@@ -102,8 +109,8 @@ class TestPylibUtilMethods(unittest.TestCase):
[1.0000000005, "1.00000s"],
# -1.0000000005 s rounds to 1s
[-1.0000000005, "-1.0000s"],
- # 0.999999 s round to 1s
- [0.999999, "1.00000s"],
+ # 0.9999999999 s round to 1s
+ [0.9999999999, "1.00000s"],
# 0.00000000049 s rounds to 0ns
[0.00000000049, " 0ns"],
# 0.00000000051 s rounds to 1ns
@@ -115,22 +122,24 @@ class TestPylibUtilMethods(unittest.TestCase):
# 0.4 ms rounds to 0ns
self.assertEqual(f(0.0000004, usec, ntp.util.UNIT_MS),
- " 0ns") # Checking timefuzz
+ " 0ns")
# 0.5 ms rounds to 1ns
self.assertEqual(f(0.0000005, usec, ntp.util.UNIT_MS),
- " 1ns") # Checking timefuzz
- # 0.5 ms rounds to 1ns
+ " 1ns")
+ # 0.5 ms rounds to 1ns, with strip
self.assertEqual(f(0.0000005, usec, ntp.util.UNIT_MS, strip=True),
- "0ns") # Checking timefuzz, strip
+ "1ns")
self.assertEqual(f(12.45, usec, ntp.util.UNIT_MS),
- " 12.45ms") # Checking normal
+ "12.450ms") # Checking normal
self.assertEqual(f(12.45, usec, ntp.util.UNIT_MS, strip=True),
- "12.45ms") # Checking normal, strip
+ "12.450ms") # Checking normal, strip
# 12.499999 ms rounds to 12.45 ms
- self.assertEqual(f(12.499999, usec, ntp.util.UNIT_MS, strip=True),
- "12.45ms") # Checking normal, strip
+ self.assertEqual(f(12.499999, usec, ntp.util.UNIT_MS,
+ strip=True, width=6),
+ "12.5ms") # Checking normal, strip
# 12.451 ms rounds to 12.45 ms
- self.assertEqual(f(12.451, usec, ntp.util.UNIT_MS, strip=True),
+ self.assertEqual(f(12.451, usec, ntp.util.UNIT_MS,
+ strip=True, width=7),
"12.45ms") # Checking normal, strip
self.assertEqual(f(123456789.1234, usec, ntp.util.UNIT_MS, width=None),
"123.4567891234ks") # Checking normal, no width
=====================================
tests/wscript
=====================================
--- a/tests/wscript
+++ b/tests/wscript
@@ -75,7 +75,7 @@ def build(ctx):
includes=["%s/tests/unity/" % srcnode,
"%s/ntpd/" % srcnode,
"%s/tests/libntp/" % srcnode,
- "%s/tests/common/" % srcnode
+ "%s/tests/common/" % srcnode,
],
install_path=None,
source=ntpd_source,
@@ -83,3 +83,6 @@ def build(ctx):
use="ntpd_lib libntpd_obj unity ntp isc "
"M PTHREAD CRYPTO RT SOCKET NSL",
)
+
+ ctx.exec_command("%s/tests/pylib/test_statfiles.py" % srcnode)
+ ctx.exec_command("%s/tests/pylib/test_util.py" % srcnode)
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/6282a87ace08f8301bfacfc0830e573e4e3cdd03...5b70d40df3e40e40d9a52fb35f1433d2526885e9
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170404/5064175e/attachment.html>
More information about the vc
mailing list