Skip to content

Commit 2a78ad9

Browse files
committed
Revert RKbug.py for now
* The diff was way too big. Why??
1 parent 999fdb5 commit 2a78ad9

File tree

1 file changed

+147
-155
lines changed

1 file changed

+147
-155
lines changed

tests/RKbug.py

Lines changed: 147 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,147 @@
1-
from __future__ import print_function
2-
from __future__ import absolute_import
3-
from __future__ import division
4-
5-
import sys
6-
from struct import pack, unpack
7-
8-
from future import *
9-
10-
from xlwt import *
11-
12-
13-
def cellname(rowx, colx):
14-
# quick kludge, works up to 26 cols :-)
15-
return chr(ord('A') + colx) + str(rowx + 1)
16-
17-
def RK_pack_check(num, anint, case=None):
18-
if not(-0x7fffffff - 1 <= anint <= 0x7fffffff):
19-
print("RK_pack_check: not a signed 32-bit int: %r (%r); case: %r" \
20-
% (anint, hex(anint), case))
21-
pstr = pack("<i", anint)
22-
actual = unpack_RK(pstr)
23-
if actual != num:
24-
print("RK_pack_check: round trip failure: %r (%r); case %r; %r in, %r out" \
25-
% (anint, hex(anint), case, num, actual))
26-
27-
28-
def RK_encode(num, blah=0):
29-
"""\
30-
Return a 4-byte RK encoding of the input float value
31-
if possible, else return None.
32-
"""
33-
rk_encoded = 0
34-
packed = pack('<d', num)
35-
36-
if blah: print()
37-
if blah: print(repr(num))
38-
w01, w23 = unpack('<2i', packed)
39-
if not w01 and not(w23 & 3):
40-
# 34 lsb are 0
41-
if blah: print("float RK", w23, hex(w23))
42-
return RK_pack_check(num, w23, 0)
43-
# return RKRecord(
44-
# self.__parent.get_index(), self.__idx, self.__xf_idx, w23).get()
45-
46-
if -0x20000000 <= num < 0x20000000:
47-
inum = int(num)
48-
if inum == num:
49-
if blah: print("30-bit integer RK", inum, hex(inum))
50-
rk_encoded = 2 | (inum << 2)
51-
if blah: print("rk", rk_encoded, hex(rk_encoded))
52-
return RK_pack_check(num, rk_encoded, 2)
53-
# return RKRecord(
54-
# self.__parent.get_index(), self.__idx, self.__xf_idx, rk_encoded).get()
55-
56-
temp = num * 100
57-
packed100 = pack('<d', temp)
58-
w01, w23 = unpack('<2i', packed100)
59-
if not w01 and not(w23 & 3):
60-
# 34 lsb are 0
61-
if blah: print("float RK*100", w23, hex(w23))
62-
return RK_pack_check(num, w23 | 1, 1)
63-
# return RKRecord(
64-
# self.__parent.get_index(), self.__idx, self.__xf_idx, w23 | 1).get()
65-
66-
if -0x20000000 <= temp < 0x20000000:
67-
itemp = int(round(temp, 0))
68-
if blah: print((itemp == temp), (itemp / 100.0 == num))
69-
if itemp / 100.0 == num:
70-
if blah: print("30-bit integer RK*100", itemp, hex(itemp))
71-
rk_encoded = 3 | (itemp << 2)
72-
return RK_pack_check(num, rk_encoded, 3)
73-
# return RKRecord(
74-
# self.__parent.get_index(), self.__idx, self.__xf_idx, rk_encoded).get()
75-
76-
if blah: print("Number")
77-
# return NumberRecord(
78-
# self.__parent.get_index(), self.__idx, self.__xf_idx, num).get()
79-
80-
def unpack_RK(rk_str):
81-
flags = ord(rk_str[0])
82-
if flags & 2:
83-
# There's a SIGNED 30-bit integer in there!
84-
i, = unpack('<i', rk_str)
85-
i >>= 2 # div by 4 to drop the 2 flag bits
86-
if flags & 1:
87-
return i / 100.0
88-
return float(i)
89-
else:
90-
# It's the most significant 30 bits of an IEEE 754 64-bit FP number
91-
d, = unpack('<d', '\0\0\0\0' + chr(flags & 252) + rk_str[1:4])
92-
if flags & 1:
93-
return d / 100.0
94-
return d
95-
96-
testvals = (
97-
130.63999999999999,
98-
130.64,
99-
-18.649999999999999,
100-
-18.65,
101-
137.19999999999999,
102-
137.20,
103-
-16.079999999999998,
104-
-16.08,
105-
0,
106-
1,
107-
2,
108-
3,
109-
0x1fffffff,
110-
0x20000000,
111-
0x20000001,
112-
1000999999,
113-
0x3fffffff,
114-
0x40000000,
115-
0x40000001,
116-
0x7fffffff,
117-
0x80000000,
118-
0x80000001,
119-
0xffffffff,
120-
0x100000000,
121-
0x100000001,
122-
)
123-
124-
XLS = 1
125-
BLAH = 1
126-
127-
def main(do_neg):
128-
if XLS:
129-
w = Workbook()
130-
ws = w.add_sheet('Test RK encoding')
131-
for colx, heading in enumerate(('actual', 'expected', 'OK') * 2):
132-
ws.write(0, colx, heading)
133-
rx = 0
134-
for neg in range(do_neg + 1):
135-
for seed in testvals:
136-
rx += 1
137-
for i in range(2):
138-
bv = [seed, seed /100.00][i] * (1 - 2 * neg)
139-
bv = float(bv) # pyExcelerator cracks it with longs
140-
cx = i * 3
141-
if XLS:
142-
ws.write(rx, cx, bv)
143-
ws.write(rx, cx + 1, repr(bv))
144-
ws.write(rx, cx + 2, Formula(
145-
'%s=VALUE(%s)' % (cellname(rx, cx), cellname(rx, cx + 1))
146-
))
147-
else:
148-
RK_encode(bv, blah=BLAH)
149-
if XLS:
150-
w.save('RKbug%d.xls' % do_neg)
151-
152-
if __name__ == "__main__":
153-
# arg == 0: only positive test values used
154-
# arg == 1: both positive & negative test values used
155-
main(int(sys.argv[1]))
1+
from xlwt import *
2+
import sys
3+
from struct import pack, unpack
4+
5+
def cellname(rowx, colx):
6+
# quick kludge, works up to 26 cols :-)
7+
return chr(ord('A') + colx) + str(rowx + 1)
8+
9+
def RK_pack_check(num, anint, case=None):
10+
if not(-0x7fffffff - 1 <= anint <= 0x7fffffff):
11+
print "RK_pack_check: not a signed 32-bit int: %r (%r); case: %r" \
12+
% (anint, hex(anint), case)
13+
pstr = pack("<i", anint)
14+
actual = unpack_RK(pstr)
15+
if actual != num:
16+
print "RK_pack_check: round trip failure: %r (%r); case %r; %r in, %r out" \
17+
% (anint, hex(anint), case, num, actual)
18+
19+
20+
def RK_encode(num, blah=0):
21+
"""\
22+
Return a 4-byte RK encoding of the input float value
23+
if possible, else return None.
24+
"""
25+
rk_encoded = 0
26+
packed = pack('<d', num)
27+
28+
if blah: print
29+
if blah: print repr(num)
30+
w01, w23 = unpack('<2i', packed)
31+
if not w01 and not(w23 & 3):
32+
# 34 lsb are 0
33+
if blah: print "float RK", w23, hex(w23)
34+
return RK_pack_check(num, w23, 0)
35+
# return RKRecord(
36+
# self.__parent.get_index(), self.__idx, self.__xf_idx, w23).get()
37+
38+
if -0x20000000 <= num < 0x20000000:
39+
inum = int(num)
40+
if inum == num:
41+
if blah: print "30-bit integer RK", inum, hex(inum)
42+
rk_encoded = 2 | (inum << 2)
43+
if blah: print "rk", rk_encoded, hex(rk_encoded)
44+
return RK_pack_check(num, rk_encoded, 2)
45+
# return RKRecord(
46+
# self.__parent.get_index(), self.__idx, self.__xf_idx, rk_encoded).get()
47+
48+
temp = num * 100
49+
packed100 = pack('<d', temp)
50+
w01, w23 = unpack('<2i', packed100)
51+
if not w01 and not(w23 & 3):
52+
# 34 lsb are 0
53+
if blah: print "float RK*100", w23, hex(w23)
54+
return RK_pack_check(num, w23 | 1, 1)
55+
# return RKRecord(
56+
# self.__parent.get_index(), self.__idx, self.__xf_idx, w23 | 1).get()
57+
58+
if -0x20000000 <= temp < 0x20000000:
59+
itemp = int(round(temp, 0))
60+
if blah: print (itemp == temp), (itemp / 100.0 == num)
61+
if itemp / 100.0 == num:
62+
if blah: print "30-bit integer RK*100", itemp, hex(itemp)
63+
rk_encoded = 3 | (itemp << 2)
64+
return RK_pack_check(num, rk_encoded, 3)
65+
# return RKRecord(
66+
# self.__parent.get_index(), self.__idx, self.__xf_idx, rk_encoded).get()
67+
68+
if blah: print "Number"
69+
# return NumberRecord(
70+
# self.__parent.get_index(), self.__idx, self.__xf_idx, num).get()
71+
72+
def unpack_RK(rk_str):
73+
flags = ord(rk_str[0])
74+
if flags & 2:
75+
# There's a SIGNED 30-bit integer in there!
76+
i, = unpack('<i', rk_str)
77+
i >>= 2 # div by 4 to drop the 2 flag bits
78+
if flags & 1:
79+
return i / 100.0
80+
return float(i)
81+
else:
82+
# It's the most significant 30 bits of an IEEE 754 64-bit FP number
83+
d, = unpack('<d', '\0\0\0\0' + chr(flags & 252) + rk_str[1:4])
84+
if flags & 1:
85+
return d / 100.0
86+
return d
87+
88+
testvals = (
89+
130.63999999999999,
90+
130.64,
91+
-18.649999999999999,
92+
-18.65,
93+
137.19999999999999,
94+
137.20,
95+
-16.079999999999998,
96+
-16.08,
97+
0,
98+
1,
99+
2,
100+
3,
101+
0x1fffffff,
102+
0x20000000,
103+
0x20000001,
104+
1000999999,
105+
0x3fffffff,
106+
0x40000000,
107+
0x40000001,
108+
0x7fffffff,
109+
0x80000000,
110+
0x80000001,
111+
0xffffffff,
112+
0x100000000,
113+
0x100000001,
114+
)
115+
116+
XLS = 1
117+
BLAH = 1
118+
119+
def main(do_neg):
120+
if XLS:
121+
w = Workbook()
122+
ws = w.add_sheet('Test RK encoding')
123+
for colx, heading in enumerate(('actual', 'expected', 'OK') * 2):
124+
ws.write(0, colx, heading)
125+
rx = 0
126+
for neg in range(do_neg + 1):
127+
for seed in testvals:
128+
rx += 1
129+
for i in range(2):
130+
bv = [seed, seed /100.00][i] * (1 - 2 * neg)
131+
bv = float(bv) # pyExcelerator cracks it with longs
132+
cx = i * 3
133+
if XLS:
134+
ws.write(rx, cx, bv)
135+
ws.write(rx, cx + 1, repr(bv))
136+
ws.write(rx, cx + 2, Formula(
137+
'%s=VALUE(%s)' % (cellname(rx, cx), cellname(rx, cx + 1))
138+
))
139+
else:
140+
RK_encode(bv, blah=BLAH)
141+
if XLS:
142+
w.save('RKbug%d.xls' % do_neg)
143+
144+
if __name__ == "__main__":
145+
# arg == 0: only positive test values used
146+
# arg == 1: both positive & negative test values used
147+
main(int(sys.argv[1]))

0 commit comments

Comments
 (0)