this repo has no description
at trunk 188 lines 4.5 kB view raw
1# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) 2"""Tests for __static__ module. 3 4These are tests for the plain-Python fallback implementations in __static__; 5the static-Python uses are tested in test_compiler/test_static.py. 6 7""" 8import unittest 9from __static__ import ( 10 Array, 11 Vector, 12 byte, 13 char, 14 cbool, 15 int8, 16 int16, 17 int32, 18 int64, 19 uint8, 20 uint16, 21 uint32, 22 uint64, 23 single, 24 double, 25 size_t, 26 ssize_t, 27 allow_weakrefs, 28 dynamic_return, 29 box, 30 unbox, 31 cast, 32 CheckedDict, 33 chkdict, 34 pydict, 35 PyDict, 36 clen, 37 rand, 38 RAND_MAX 39) 40from typing import Generic, Optional, TypeVar, Union, Dict 41 42try: 43 import _static 44except ImportError: 45 _static = None 46 47 48class StaticTests(unittest.TestCase): 49 def test_chkdict(self): 50 tgt = dict if _static is None else _static.chkdict 51 self.assertIs(CheckedDict, tgt) 52 self.assertIs(chkdict, tgt) 53 54 def test_pydict(self): 55 self.assertIs(pydict, dict) 56 self.assertIs(PyDict, Dict) 57 58 def test_clen(self): 59 self.assertIs(clen, len) 60 61 def test_int_types(self): 62 for typ in [ 63 size_t, 64 ssize_t, 65 int8, 66 uint8, 67 int16, 68 uint16, 69 int32, 70 uint32, 71 int64, 72 uint64, 73 byte, 74 char, 75 cbool, 76 ]: 77 with self.subTest(typ=typ): 78 x = typ(1) 79 self.assertEqual(x, 1) 80 81 def test_float_types(self): 82 for typ in [ 83 single, 84 double, 85 ]: 86 with self.subTest(typ=typ): 87 x = typ(1.0) 88 self.assertEqual(x, 1.0) 89 90 def test_array(self): 91 x = Array[int8]([1]) 92 self.assertEqual(x[0], 1) 93 94 def test_array_size(self): 95 with self.assertRaisesRegex(OverflowError, "greater than maximum"): 96 x = Array[int8]([300]) 97 98 def test_array_no_bare_instantiation(self): 99 with self.assertRaisesRegex(TypeError, "Cannot create plain Array"): 100 Array(42) 101 102 def test_array_no_subclass(self): 103 with self.assertRaisesRegex(TypeError, "Cannot subclass Array"): 104 class MyArray(Array): 105 pass 106 107 def test_vector(self): 108 x = Vector[int8]([1]) 109 self.assertEqual(x[0], 1) 110 x.append(5) 111 self.assertEqual(x[1], 5) 112 113 def test_vector_size(self): 114 x = Vector[int8]() 115 with self.assertRaisesRegex(OverflowError, "overflow|.*greater than maximum"): 116 x.append(300) 117 118 def test_vector_no_bare_instantiation(self): 119 with self.assertRaisesRegex(TypeError, "Cannot create plain Vector"): 120 Vector() 121 122 def test_vector_no_subclass(self): 123 with self.assertRaisesRegex(TypeError, "Cannot subclass Vector"): 124 class MyVec(Vector): 125 pass 126 127 def test_box(self): 128 self.assertEqual(box(1), 1) 129 130 def test_unbox(self): 131 self.assertEqual(unbox(1), 1) 132 133 def test_allow_weakrefs(self): 134 class MyClass: 135 pass 136 137 self.assertIs(MyClass, allow_weakrefs(MyClass)) 138 139 def test_dynamic_return(self): 140 def foo(): 141 pass 142 143 self.assertIs(foo, dynamic_return(foo)) 144 145 def test_cast(self): 146 self.assertIs(cast(int, 2), 2) 147 148 def test_cast_subtype(self): 149 class Base: 150 pass 151 152 class Sub(Base): 153 pass 154 155 s = Sub() 156 self.assertIs(cast(Base, s), s) 157 158 def test_cast_fail(self): 159 with self.assertRaisesRegex(TypeError, "expected int, got str"): 160 cast(int, "foo") 161 162 def test_cast_optional(self): 163 self.assertIs(cast(Optional[int], None), None) 164 self.assertIs(cast(int | None, None), None) 165 self.assertIs(cast(None | int, None), None) 166 167 def test_cast_generic_type(self): 168 T = TypeVar("T") 169 170 class G(Generic[T]): 171 pass 172 173 g = G() 174 175 self.assertIs(cast(G[int], g), g) 176 177 def test_cast_type_too_complex(self): 178 with self.assertRaisesRegex(ValueError, r"cast expects type or Optional\[T\]"): 179 cast(Union[int, str], int) 180 181 def test_rand(self): 182 self.assertEqual(type(RAND_MAX), int) 183 self.assertLessEqual(rand(), RAND_MAX) 184 self.assertGreaterEqual(rand(), 0) 185 186 187if __name__ == "__main__": 188 unittest.main()