Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_bitsizeof.py: 100%

225 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-12-05 10:43 +0000

1import unittest 

2 

3from zserio.bitbuffer import BitBuffer 

4from zserio.bitsizeof import ( 

5 bitsizeof_varint16, 

6 bitsizeof_varint32, 

7 bitsizeof_varint64, 

8 bitsizeof_varint, 

9 bitsizeof_varuint16, 

10 bitsizeof_varuint32, 

11 bitsizeof_varuint64, 

12 bitsizeof_varuint, 

13 bitsizeof_varsize, 

14 bitsizeof_string, 

15 bitsizeof_bitbuffer, 

16) 

17from zserio.exception import PythonRuntimeException 

18from zserio.limits import INT64_MIN 

19 

20 

21class BitSizeOfTest(unittest.TestCase): 

22 

23 def test_bitsizeof_varint16(self): 

24 self.assertEqual(1 * 8, bitsizeof_varint16(0)) 

25 

26 self.assertEqual(1 * 8, bitsizeof_varint16(1 << (0))) 

27 self.assertEqual(1 * 8, bitsizeof_varint16(-(1 << (0)))) 

28 self.assertEqual(1 * 8, bitsizeof_varint16((1 << (6)) - 1)) 

29 self.assertEqual(1 * 8, bitsizeof_varint16(-((1 << (6)) - 1))) 

30 

31 self.assertEqual(2 * 8, bitsizeof_varint16(1 << (6))) 

32 self.assertEqual(2 * 8, bitsizeof_varint16(-(1 << (6)))) 

33 self.assertEqual(2 * 8, bitsizeof_varint16((1 << (6 + 8)) - 1)) 

34 self.assertEqual(2 * 8, bitsizeof_varint16(-((1 << (6 + 8)) - 1))) 

35 

36 with self.assertRaises(PythonRuntimeException): 

37 bitsizeof_varint16(-(1 << (6 + 8))) # below the lower bound 

38 

39 with self.assertRaises(PythonRuntimeException): 

40 bitsizeof_varint16(1 << (6 + 8)) # above the upper bound 

41 

42 def test_bitsizeof_varint32(self): 

43 self.assertEqual(1 * 8, bitsizeof_varint32(0)) 

44 

45 self.assertEqual(1 * 8, bitsizeof_varint32(1 << (0))) 

46 self.assertEqual(1 * 8, bitsizeof_varint32(-(1 << (0)))) 

47 self.assertEqual(1 * 8, bitsizeof_varint32((1 << (6)) - 1)) 

48 self.assertEqual(1 * 8, bitsizeof_varint32(-((1 << (6)) - 1))) 

49 

50 self.assertEqual(2 * 8, bitsizeof_varint32(1 << (6))) 

51 self.assertEqual(2 * 8, bitsizeof_varint32(-(1 << (6)))) 

52 self.assertEqual(2 * 8, bitsizeof_varint32((1 << (6 + 7)) - 1)) 

53 self.assertEqual(2 * 8, bitsizeof_varint32(-((1 << (6 + 7)) - 1))) 

54 

55 self.assertEqual(3 * 8, bitsizeof_varint32(1 << (6 + 7))) 

56 self.assertEqual(3 * 8, bitsizeof_varint32(-(1 << (6 + 7)))) 

57 self.assertEqual(3 * 8, bitsizeof_varint32((1 << (6 + 7 + 7)) - 1)) 

58 self.assertEqual(3 * 8, bitsizeof_varint32(-((1 << (6 + 7 + 7)) - 1))) 

59 

60 self.assertEqual(4 * 8, bitsizeof_varint32(1 << (6 + 7 + 7))) 

61 self.assertEqual(4 * 8, bitsizeof_varint32(-(1 << (6 + 7 + 7)))) 

62 self.assertEqual(4 * 8, bitsizeof_varint32((1 << (6 + 7 + 7 + 8)) - 1)) 

63 self.assertEqual(4 * 8, bitsizeof_varint32(-((1 << (6 + 7 + 7 + 8)) - 1))) 

64 

65 with self.assertRaises(PythonRuntimeException): 

66 bitsizeof_varint32(-(1 << (6 + 7 + 7 + 8))) # below the lower bound 

67 

68 with self.assertRaises(PythonRuntimeException): 

69 bitsizeof_varint32(1 << (6 + 7 + 7 + 8)) # above the upper bound 

70 

71 def test_bitsizeof_varint64(self): 

72 self.assertEqual(1 * 8, bitsizeof_varint64(0)) 

73 

74 self.assertEqual(1 * 8, bitsizeof_varint64(1 << (0))) 

75 self.assertEqual(1 * 8, bitsizeof_varint64(-(1 << (0)))) 

76 self.assertEqual(1 * 8, bitsizeof_varint64((1 << (6)) - 1)) 

77 self.assertEqual(1 * 8, bitsizeof_varint64(-((1 << (6)) - 1))) 

78 

79 self.assertEqual(2 * 8, bitsizeof_varint64(1 << (6))) 

80 self.assertEqual(2 * 8, bitsizeof_varint64(-(1 << (6)))) 

81 self.assertEqual(2 * 8, bitsizeof_varint64((1 << (6 + 7)) - 1)) 

82 self.assertEqual(2 * 8, bitsizeof_varint64(-((1 << (6 + 7)) - 1))) 

83 

84 self.assertEqual(3 * 8, bitsizeof_varint64(1 << (6 + 7))) 

85 self.assertEqual(3 * 8, bitsizeof_varint64(-(1 << (6 + 7)))) 

86 self.assertEqual(3 * 8, bitsizeof_varint64((1 << (6 + 7 + 7)) - 1)) 

87 self.assertEqual(3 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7)) - 1))) 

88 

89 self.assertEqual(4 * 8, bitsizeof_varint64(1 << (6 + 7 + 7))) 

90 self.assertEqual(4 * 8, bitsizeof_varint64(-(1 << (6 + 7 + 7)))) 

91 self.assertEqual(4 * 8, bitsizeof_varint64((1 << (6 + 7 + 7 + 7)) - 1)) 

92 self.assertEqual(4 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7 + 7)) - 1))) 

93 

94 self.assertEqual(5 * 8, bitsizeof_varint64(1 << (6 + 7 + 7 + 7))) 

95 self.assertEqual(5 * 8, bitsizeof_varint64(-(1 << (6 + 7 + 7 + 7)))) 

96 self.assertEqual(5 * 8, bitsizeof_varint64((1 << (6 + 7 + 7 + 7 + 7)) - 1)) 

97 self.assertEqual(5 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7 + 7 + 7)) - 1))) 

98 

99 self.assertEqual(6 * 8, bitsizeof_varint64(1 << (6 + 7 + 7 + 7 + 7))) 

100 self.assertEqual(6 * 8, bitsizeof_varint64(-(1 << (6 + 7 + 7 + 7 + 7)))) 

101 self.assertEqual(6 * 8, bitsizeof_varint64((1 << (6 + 7 + 7 + 7 + 7 + 7)) - 1)) 

102 self.assertEqual(6 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7 + 7 + 7 + 7)) - 1))) 

103 

104 self.assertEqual(7 * 8, bitsizeof_varint64(1 << (6 + 7 + 7 + 7 + 7 + 7))) 

105 self.assertEqual(7 * 8, bitsizeof_varint64(-(1 << (6 + 7 + 7 + 7 + 7 + 7)))) 

106 self.assertEqual(7 * 8, bitsizeof_varint64((1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1)) 

107 self.assertEqual(7 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)) - 1))) 

108 

109 self.assertEqual(8 * 8, bitsizeof_varint64(1 << (6 + 7 + 7 + 7 + 7 + 7 + 7))) 

110 self.assertEqual(8 * 8, bitsizeof_varint64(-(1 << (6 + 7 + 7 + 7 + 7 + 7 + 7)))) 

111 self.assertEqual(8 * 8, bitsizeof_varint64((1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1)) 

112 self.assertEqual(8 * 8, bitsizeof_varint64(-((1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1))) 

113 

114 with self.assertRaises(PythonRuntimeException): 

115 bitsizeof_varint64(-(1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8))) # below the lower bound 

116 

117 with self.assertRaises(PythonRuntimeException): 

118 bitsizeof_varint64(1 << (6 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) # above the upper bound 

119 

120 def test_bitsizeof_varint(self): 

121 self.assertEqual(8, bitsizeof_varint(0)) 

122 self.assertEqual(8, bitsizeof_varint(-(1 << 6) + 1)) 

123 self.assertEqual(8, bitsizeof_varint((1 << 6) - 1)) 

124 self.assertEqual(16, bitsizeof_varint(-(1 << 6))) 

125 self.assertEqual(16, bitsizeof_varint((1 << 6))) 

126 self.assertEqual(16, bitsizeof_varint(-(1 << 13) + 1)) 

127 self.assertEqual(16, bitsizeof_varint((1 << 13) - 1)) 

128 self.assertEqual(24, bitsizeof_varint(-(1 << 13))) 

129 self.assertEqual(24, bitsizeof_varint((1 << 13))) 

130 self.assertEqual(24, bitsizeof_varint(-(1 << 20) + 1)) 

131 self.assertEqual(24, bitsizeof_varint((1 << 20) - 1)) 

132 self.assertEqual(32, bitsizeof_varint(-(1 << 20))) 

133 self.assertEqual(32, bitsizeof_varint((1 << 20))) 

134 self.assertEqual(32, bitsizeof_varint(-(1 << 27) + 1)) 

135 self.assertEqual(32, bitsizeof_varint((1 << 27) - 1)) 

136 self.assertEqual(40, bitsizeof_varint(-(1 << 27))) 

137 self.assertEqual(40, bitsizeof_varint((1 << 27))) 

138 self.assertEqual(40, bitsizeof_varint(-(1 << 34) + 1)) 

139 self.assertEqual(40, bitsizeof_varint((1 << 34) - 1)) 

140 self.assertEqual(48, bitsizeof_varint(-(1 << 34))) 

141 self.assertEqual(48, bitsizeof_varint((1 << 34))) 

142 self.assertEqual(48, bitsizeof_varint(-(1 << 41) + 1)) 

143 self.assertEqual(48, bitsizeof_varint((1 << 41) - 1)) 

144 self.assertEqual(56, bitsizeof_varint(-(1 << 41))) 

145 self.assertEqual(56, bitsizeof_varint((1 << 41))) 

146 self.assertEqual(56, bitsizeof_varint(-(1 << 48) + 1)) 

147 self.assertEqual(56, bitsizeof_varint((1 << 48) - 1)) 

148 self.assertEqual(64, bitsizeof_varint(-(1 << 48))) 

149 self.assertEqual(64, bitsizeof_varint((1 << 48))) 

150 self.assertEqual(64, bitsizeof_varint(-(1 << 55) + 1)) 

151 self.assertEqual(64, bitsizeof_varint((1 << 55) - 1)) 

152 self.assertEqual(72, bitsizeof_varint(-(1 << 55))) 

153 self.assertEqual(72, bitsizeof_varint((1 << 55))) 

154 self.assertEqual(72, bitsizeof_varint(-(1 << 63) + 1)) 

155 self.assertEqual(72, bitsizeof_varint((1 << 63) - 1)) 

156 

157 # special case, INT64_MIN is stored as -0 

158 self.assertEqual(8, bitsizeof_varint(INT64_MIN)) 

159 

160 with self.assertRaises(PythonRuntimeException): 

161 bitsizeof_varint(INT64_MIN - 1) # below the lower bound 

162 

163 with self.assertRaises(PythonRuntimeException): 

164 bitsizeof_varint(1 << 63) # above the upper bound 

165 

166 def test_bitsizeof_varuint16(self): 

167 self.assertEqual(1 * 8, bitsizeof_varuint16(0)) 

168 

169 self.assertEqual(1 * 8, bitsizeof_varuint16(1 << (0))) 

170 self.assertEqual(1 * 8, bitsizeof_varuint16((1 << (7)) - 1)) 

171 

172 self.assertEqual(2 * 8, bitsizeof_varuint16(1 << (7))) 

173 self.assertEqual(2 * 8, bitsizeof_varuint16((1 << (7 + 8)) - 1)) 

174 

175 with self.assertRaises(PythonRuntimeException): 

176 bitsizeof_varuint16(-1) # below the lower bound 

177 

178 with self.assertRaises(PythonRuntimeException): 

179 bitsizeof_varuint16(1 << (7 + 8)) # above the upper bound 

180 

181 def test_bitsizeof_varuint32(self): 

182 self.assertEqual(1 * 8, bitsizeof_varuint32(0)) 

183 

184 self.assertEqual(1 * 8, bitsizeof_varuint32(1 << (0))) 

185 self.assertEqual(1 * 8, bitsizeof_varuint32((1 << (7)) - 1)) 

186 

187 self.assertEqual(2 * 8, bitsizeof_varuint32(1 << (7))) 

188 self.assertEqual(2 * 8, bitsizeof_varuint32((1 << (7 + 7)) - 1)) 

189 

190 self.assertEqual(3 * 8, bitsizeof_varuint32(1 << (7 + 7))) 

191 self.assertEqual(3 * 8, bitsizeof_varuint32((1 << (7 + 7 + 7)) - 1)) 

192 

193 self.assertEqual(4 * 8, bitsizeof_varuint32(1 << (7 + 7 + 7))) 

194 self.assertEqual(4 * 8, bitsizeof_varuint32((1 << (7 + 7 + 7 + 8)) - 1)) 

195 

196 with self.assertRaises(PythonRuntimeException): 

197 bitsizeof_varuint32(-1) # below the lower bound 

198 

199 with self.assertRaises(PythonRuntimeException): 

200 bitsizeof_varuint32(1 << (7 + 7 + 7 + 8)) # above the upper bound 

201 

202 def test_bitsizeof_varuint64(self): 

203 self.assertEqual(1 * 8, bitsizeof_varuint64(0)) 

204 

205 self.assertEqual(1 * 8, bitsizeof_varuint64(1 << (0))) 

206 self.assertEqual(1 * 8, bitsizeof_varuint64((1 << (7)) - 1)) 

207 

208 self.assertEqual(2 * 8, bitsizeof_varuint64(1 << (7))) 

209 self.assertEqual(2 * 8, bitsizeof_varuint64((1 << (7 + 7)) - 1)) 

210 

211 self.assertEqual(3 * 8, bitsizeof_varuint64(1 << (7 + 7))) 

212 self.assertEqual(3 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7)) - 1)) 

213 

214 self.assertEqual(4 * 8, bitsizeof_varuint64(1 << (7 + 7 + 7))) 

215 self.assertEqual(4 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7 + 7)) - 1)) 

216 

217 self.assertEqual(5 * 8, bitsizeof_varuint64(1 << (7 + 7 + 7 + 7))) 

218 self.assertEqual(5 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7 + 7 + 7)) - 1)) 

219 

220 self.assertEqual(6 * 8, bitsizeof_varuint64(1 << (7 + 7 + 7 + 7 + 7))) 

221 self.assertEqual(6 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7 + 7 + 7 + 7)) - 1)) 

222 

223 self.assertEqual(7 * 8, bitsizeof_varuint64(1 << (7 + 7 + 7 + 7 + 7 + 7))) 

224 self.assertEqual(7 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7 + 7 + 7 + 7 + 7)) - 1)) 

225 

226 self.assertEqual(8 * 8, bitsizeof_varuint64(1 << (7 + 7 + 7 + 7 + 7 + 7 + 7))) 

227 self.assertEqual(8 * 8, bitsizeof_varuint64((1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) - 1)) 

228 

229 with self.assertRaises(PythonRuntimeException): 

230 bitsizeof_varuint64(-1) # below the lower bound 

231 

232 with self.assertRaises(PythonRuntimeException): 

233 bitsizeof_varuint64(1 << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 8)) # above the upper bound 

234 

235 def test_bitsizeof_varuint(self): 

236 self.assertEqual(8, bitsizeof_varuint(0)) 

237 self.assertEqual(8, bitsizeof_varuint((1 << 7) - 1)) 

238 self.assertEqual(16, bitsizeof_varuint((1 << 7))) 

239 self.assertEqual(16, bitsizeof_varuint((1 << 14) - 1)) 

240 self.assertEqual(24, bitsizeof_varuint((1 << 14))) 

241 self.assertEqual(24, bitsizeof_varuint((1 << 21) - 1)) 

242 self.assertEqual(32, bitsizeof_varuint((1 << 21))) 

243 self.assertEqual(32, bitsizeof_varuint((1 << 28) - 1)) 

244 self.assertEqual(40, bitsizeof_varuint((1 << 28))) 

245 self.assertEqual(40, bitsizeof_varuint((1 << 35) - 1)) 

246 self.assertEqual(48, bitsizeof_varuint((1 << 35))) 

247 self.assertEqual(48, bitsizeof_varuint((1 << 42) - 1)) 

248 self.assertEqual(56, bitsizeof_varuint((1 << 42))) 

249 self.assertEqual(56, bitsizeof_varuint((1 << 49) - 1)) 

250 self.assertEqual(64, bitsizeof_varuint((1 << 49))) 

251 self.assertEqual(64, bitsizeof_varuint((1 << 56) - 1)) 

252 self.assertEqual(72, bitsizeof_varuint((1 << 56))) 

253 self.assertEqual(72, bitsizeof_varuint((1 << 64) - 1)) 

254 

255 with self.assertRaises(PythonRuntimeException): 

256 bitsizeof_varuint(-1) # below the lower bound 

257 

258 with self.assertRaises(PythonRuntimeException): 

259 bitsizeof_varuint(1 << 64) # above the upper bound 

260 

261 def test_bitsizeof_varsize(self): 

262 self.assertEqual(1 * 8, bitsizeof_varsize(0)) 

263 

264 self.assertEqual(1 * 8, bitsizeof_varsize(1 << (0))) 

265 self.assertEqual(1 * 8, bitsizeof_varsize((1 << (7)) - 1)) 

266 

267 self.assertEqual(2 * 8, bitsizeof_varsize(1 << (7))) 

268 self.assertEqual(2 * 8, bitsizeof_varsize((1 << (7 + 7)) - 1)) 

269 

270 self.assertEqual(3 * 8, bitsizeof_varsize(1 << (7 + 7))) 

271 self.assertEqual(3 * 8, bitsizeof_varsize((1 << (7 + 7 + 7)) - 1)) 

272 

273 self.assertEqual(4 * 8, bitsizeof_varsize(1 << (7 + 7 + 7))) 

274 self.assertEqual(4 * 8, bitsizeof_varsize((1 << (7 + 7 + 7 + 7)) - 1)) 

275 

276 self.assertEqual(5 * 8, bitsizeof_varsize(1 << (7 + 7 + 7 + 7))) 

277 self.assertEqual(5 * 8, bitsizeof_varsize((1 << (2 + 7 + 7 + 7 + 8)) - 1)) 

278 

279 with self.assertRaises(PythonRuntimeException): 

280 bitsizeof_varsize(-1) # below the lower bound 

281 

282 with self.assertRaises(PythonRuntimeException): 

283 bitsizeof_varsize(1 << (2 + 7 + 7 + 7 + 8)) # above the upper bound 

284 

285 def test_bitsizeof_string(self): 

286 self.assertEqual((1 + 1) * 8, bitsizeof_string("T")) 

287 self.assertEqual((1 + 4) * 8, bitsizeof_string("TEST")) 

288 

289 test_string_length = 1 << 7 # 2 bytes per character! 

290 test_string = (b"\xc2\xAB" * test_string_length).decode("utf-8") 

291 self.assertEqual((2 + 2 * test_string_length) * 8, bitsizeof_string(test_string)) 

292 

293 def test_bitsizeof_bitbuffer(self): 

294 test_bitbuffer1 = BitBuffer(bytes([0xAB, 0x03]), 8) 

295 self.assertEqual(8 + 8, bitsizeof_bitbuffer(test_bitbuffer1)) 

296 

297 test_bitbuffer2 = BitBuffer(bytes([0xAB, 0x03]), 11) 

298 self.assertEqual(8 + 11, bitsizeof_bitbuffer(test_bitbuffer2)) 

299 

300 test_bitbuffer3 = BitBuffer(bytes([0xAB, 0xCD]), 16) 

301 self.assertEqual(8 + 16, bitsizeof_bitbuffer(test_bitbuffer3)) 

302 

303 test_bitbuffer4 = BitBuffer(bytes([0xAB, 0xCD])) 

304 self.assertEqual(8 + 16, bitsizeof_bitbuffer(test_bitbuffer4)) 

305 

306 test_bitbuffer5 = BitBuffer(bytes(16 * [1]), 127) 

307 self.assertEqual(8 + 15 * 8 + 7, bitsizeof_bitbuffer(test_bitbuffer5)) 

308 

309 test_bitbuffer6 = BitBuffer(bytes(16 * [1]), 128) 

310 self.assertEqual(16 + 16 * 8, bitsizeof_bitbuffer(test_bitbuffer6))