File: test_single.py

package info (click to toggle)
pcbasic 2.0.7-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 35,416 kB
  • sloc: python: 28,411; sh: 103; makefile: 10
file content (211 lines) | stat: -rw-r--r-- 7,696 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
PC-BASIC test.single
unit tests for single precision MBF floats

(c) 2020--2023 Rob Hagemans
This file is released under the GNU GPL version 3 or later.
"""

import sys
import os
from binascii import hexlify
import unittest

from pcbasic.compat import int2byte
from pcbasic.basic.values import Values, Single


HERE = os.path.dirname(os.path.abspath(__file__))


# ALLWORD.DAT is generated like so:
# with open('input/ALLWORD.DAT', 'wb') as f:
#     for i in range(256):
#         for j in range(256):
#             f.write(int2byte(j)+int2byte(i)+'\0'+'\x80')


def open_input(name):
    """Open test input file."""
    return open(os.path.join(HERE, 'input', 'single', name), 'rb')

def open_model(name):
    """Open test model file."""
    return open(os.path.join(HERE, 'model', 'single', name), 'rb')

def open_output(name):
    """Open test output file."""
    return open(os.path.join(HERE, 'output', 'single', name), 'wb')



class TestSingle(unittest.TestCase):
    """Test frame for single-precision MBF math."""

    def setUp(self):
        """Create the Values object."""
        self._vm = Values(None, False)
        try:
            os.makedirs(os.path.join(HERE, 'output', 'single'))
        except EnvironmentError:
            pass

    def test_single(self):
        """Test MBF single representation."""
        result = []
        for i in range(0, 255):
            a = self._vm.new_single().from_int(i)
            r = self._vm.new_single().from_int(2**23)
            r.iadd(a)
            s = r.clone()
            s.view()[-1:] = int2byte(bytearray(s.view())[-1] + 8)
            t = s.clone()
            result.append(s.iadd(r).isub(t).isub(r).to_value())
        model = [1.0*_i for _i in list(range(0, -129, -1)) + list(range(127, 1, -1))]
        assert result == model

    def test_all_bytes_add(self):
        """Test adding singles, all first-byte combinations."""
        with open_input('ALLWORD.DAT') as f:
            with open_model('GWBASABY.DAT') as h:
                with open_output('ADDBYTE.DAT') as g:
                    while True:
                        buf = bytearray(f.read(4))
                        if len(buf) < 4:
                            break
                        bufl = bytearray(b'%c\0\0\x80' % buf[0])
                        bufr = bytearray(b'%c\0\0\x80' % buf[1])
                        l = Single(bufl, self._vm)
                        r = Single(bufr, self._vm)
                        out = bytes(l.iadd(r).to_bytes())
                        g.write(out)
                        inp = h.read(4)
                        assert out == inp

    def test_all_bytes_sub(self):
        """Test subtracting singles, all first-byte combinations."""
        with open_input('ALLWORD.DAT') as f:
            with open_model('GWBASSBY.DAT') as h:
                with open_output('SUBBYTE.DAT') as g:
                    while True:
                        buf = bytearray(f.read(4))
                        if len(buf) < 4:
                            break
                        bufl = bytearray(b'%c\0\0\x80' % buf[0])
                        bufr = bytearray(b'%c\0\0\x80' % buf[1])
                        l = Single(bufl, self._vm)
                        r = Single(bufr, self._vm)
                        out = bytes(l.isub(r).to_bytes())
                        g.write(out)
                        inp = h.read(4)
                        assert out == inp

    def _cmp_add_exponents(self, shift, finput, model, output):
        """Adding with various exponents."""
        r = self._vm.new_single()
        with open_input(finput) as f:
            with open_model(model) as h:
                with open_output(output) as g:
                    while True:
                        l = r
                        l.view()[3:] = int2byte(0x80+shift)
                        buf = bytearray(f.read(4))
                        if len(buf) < 4:
                            break
                        buf[2:] = b'\0\x80'
                        r = Single(buf, self._vm)
                        ll = l.clone()
                        out = bytes(l.iadd(r).to_bytes())
                        g.write(out)
                        inp = h.read(4)
                        l = ll
                        assert out == inp

    def test_exponents(self):
        """Test adding with various exponents."""
        for shift in [0,] + list(range(9, 11)):
            letter = ord('0')+shift if shift<10 else ord('A')-10+shift
            model = 'GWBASAL%c.DAT' % (letter,)
            output = 'ALLWORD%c.DAT' % (letter,)
            self._cmp_add_exponents(shift, 'ALLWORD.DAT', model, output)

    def test_exponents_low(self):
        """Test adding with various exponents."""
        for shift in range(17):
            letter = ord('0')+shift if shift<10 else ord('A')-10+shift
            model = 'GWBASLO%c.DAT' % (letter,)
            output = 'LO%c.DAT' % (letter,)
            self._cmp_add_exponents(shift, 'BYTES.DAT', model, output)

    def _comp_add_bytes(self, finput, model, output):
        """Additions on random generated byte sequences."""
        fails = {}
        r = self._vm.new_single()
        with open_input(finput) as f:
            with open_model(model) as h:
                with open_output(output) as g:
                    while True:
                        l = r
                        buf = bytearray(f.read(4))
                        if len(buf) < 4:
                            break
                        r = Single(buf, self._vm)
                        ll = l.clone()
                        out = bytes(l.iadd(r).to_bytes())
                        g.write(out)
                        inp = h.read(4)
                        if out != inp:
                            fails[hexlify(inp)] = hexlify(out)
                        l = ll
        return fails

    def test_bytes(self):
        """Test additions on random generated byte sequences."""
        fails = self._comp_add_bytes('BYTES.DAT', 'GWBASADD.DAT', 'ADD.DAT')
        # two additions are slightly different
        accepted = {
            b'920a03ce': b'930a03ce',
            b'52810dbe': b'53810dbe',
        }
        assert fails == accepted

    def test_bigbytes(self):
        """Test additions on random generated byte sequences."""
        fails = self._comp_add_bytes('BIGBYTES.DAT', 'GWBIGADD.DAT', 'BIGADD.DAT')
        accepted = {
            b'922ed14b': b'932ed14b',
            b'80c02477': b'81c02477',
            b'fe4b89df': b'ff4b89df',
            b'a9b37594': b'a8b37594',
            b'bc3e8549': b'bd3e8549',
            b'b2337a91': b'b3337a91',
            b'2ef4007a': b'2ff4007a'
        }
        assert fails == accepted

    def test_mult(self):
        """Test multiplications on random generated byte sequences."""
        r = self._vm.new_single()
        with open_input('BIGBYTES.DAT') as f:
            with open_model('GWBIGMUL.DAT') as h:
                with open_output('BIGMUL.DAT') as g:
                    while True:
                        l = r
                        buf = bytearray(f.read(4))
                        if len(buf) < 4:
                            break
                        r = Single(buf, self._vm)
                        ll = l.clone()
                        try:
                            l.imul(r)
                        except OverflowError:
                            pass
                        out = bytes(l.to_bytes())
                        g.write(out)
                        inp = h.read(4)
                        l = ll
                        assert out == inp


if __name__ == '__main__':
    unittest.main()