OILS / pyext / fastfunc_test.py View on Github | oilshell.org

83 lines, 44 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4fastfunc_test.py: Tests for fastfunc.c
5"""
6import unittest
7
8from mycpp.mylib import log
9
10import fastfunc # module under test
11
12_ = log
13
14
15class FastfuncTest(unittest.TestCase):
16
17 def testJ8Encode(self):
18 s = 'hello \xff \x01 ' + u'\u03bc " \''.encode('utf-8')
19 #print(s)
20
21 x = fastfunc.J8EncodeString(s, 0)
22 print(x)
23
24 x = fastfunc.J8EncodeString(s, 1)
25 print(x)
26
27 def testShellEncode(self):
28 s = 'hello \xff \x01 ' + u'\u03bc " \''.encode('utf-8')
29 #print(s)
30
31 x = fastfunc.ShellEncodeString(s, 0)
32 print(x)
33
34 x = fastfunc.ShellEncodeString(s, 1)
35 print(x)
36
37 def testUtf8(self):
38 s = 'hi \xff'
39 self.assertEqual(True, fastfunc.PartIsUtf8(s, 0, 3))
40 self.assertEqual(False, fastfunc.PartIsUtf8(s, 3, 4))
41
42 def testUtf8Decode(self):
43 # interface is:
44 # def Utf8DecodeOne(s: str, start: int) -> (codepoint_or_error: int, bytes_read: int)
45 # If codepoint_or_error < 0, then there was a decoding error whose absolute
46 # integer value is listed in the Utf8Error enum.
47
48 s = 'h\xE2\xA1\x80\xC5\x81'
49 self.assertEqual((ord('h'), 1), fastfunc.Utf8DecodeOne(s, 0))
50 self.assertEqual((0x2840, 3), fastfunc.Utf8DecodeOne(s, 1))
51 self.assertEqual((0x141, 2), fastfunc.Utf8DecodeOne(s, 4))
52
53 # UTF8_ERR_OVERLONG = 1
54 self.assertEqual((-1, 2), fastfunc.Utf8DecodeOne("\xC1\x81", 0))
55
56 # UTF8_ERR_SURROGATE = 2
57 self.assertEqual((-2, 3), fastfunc.Utf8DecodeOne("\xED\xBF\x80", 0))
58
59 # UTF8_ERR_TOO_LARGE = 3
60 self.assertEqual((-3, 4), fastfunc.Utf8DecodeOne("\xF4\xA0\x80\x80", 0))
61
62 # UTF8_ERR_BAD_ENCODING = 4
63 self.assertEqual((-4, 2), fastfunc.Utf8DecodeOne("\xC2\xFF", 0))
64
65 # UTF8_ERR_TRUNCATED_BYTES = 5
66 self.assertEqual((-5, 1), fastfunc.Utf8DecodeOne("\xC2", 0))
67
68 def testCanOmit(self):
69 self.assertEqual(True, fastfunc.CanOmitQuotes('foo'))
70 self.assertEqual(False, fastfunc.CanOmitQuotes('foo bar'))
71 self.assertEqual(True, fastfunc.CanOmitQuotes('my-dir/my_file.cc'))
72 self.assertEqual(False, fastfunc.CanOmitQuotes(''))
73
74 self.assertEqual(False, fastfunc.CanOmitQuotes('true'))
75 self.assertEqual(False, fastfunc.CanOmitQuotes('false'))
76 self.assertEqual(False, fastfunc.CanOmitQuotes('null'))
77
78 self.assertEqual(True, fastfunc.CanOmitQuotes('truez'))
79 self.assertEqual(True, fastfunc.CanOmitQuotes('nul'))
80
81
82if __name__ == '__main__':
83 unittest.main()