def testRaisesWhenBufferIsTooLong(self): with self.assertRaisesRegex(ValueError, "Too many bytes when decoding varint"): rdf_structs.VarintReader( b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01\x00\x00", 0) with self.assertRaisesRegex(ValueError, "Too many bytes when decoding varint"): rdf_structs.VarintReader( b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0)
def testReaderCanReadWhatEncoderHasEncoded(self): for v in [ 0, 1, 2, 10, 20, 63, 64, 65, 127, 128, 129, 255, 256, 257, 1 << 63 - 1, 1 << 64 - 1 ]: with self.subTest(value=v): buf = rdf_structs.VarintEncode(v) r, p = rdf_structs.VarintReader(buf, 0) self.assertEqual(v, r) self.assertLen(buf, p)
def testZero(self): with self.assertRaisesRegex(ValueError, "Too many bytes when decoding varint"): rdf_structs.VarintReader(b"", 0)
def testNonCanonicalZeroDoesNotRaise(self): self.assertEqual(rdf_structs.VarintReader(b"\x80\x80\x80\x00", 0), (0, 4))
def testDecodingValueLargerThan64BitReturnsTruncatedValue(self): self.assertEqual( rdf_structs.VarintReader( b"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02", 0), (0, 10))
def testCorrectlyReadsEncodedIntegers(self): for src_val, b in self._ENCODED_VARINTS: with self.subTest(buffer=b): self.assertEqual(rdf_structs.VarintReader(b, 0), (src_val, len(b)))