1import torch 2 3 4def check_error(desc, fn, *required_substrings): 5 try: 6 fn() 7 except Exception as e: 8 error_message = e.args[0] 9 print("=" * 80) 10 print(desc) 11 print("-" * 80) 12 print(error_message) 13 print() 14 for sub in required_substrings: 15 assert sub in error_message 16 return 17 raise AssertionError(f"given function ({desc}) didn't raise an error") 18 19 20check_error("Wrong argument types", lambda: torch.FloatStorage(object()), "object") 21 22check_error( 23 "Unknown keyword argument", lambda: torch.FloatStorage(content=1234.0), "keyword" 24) 25 26check_error( 27 "Invalid types inside a sequence", 28 lambda: torch.FloatStorage(["a", "b"]), 29 "list", 30 "str", 31) 32 33check_error("Invalid size type", lambda: torch.FloatStorage(1.5), "float") 34 35check_error( 36 "Invalid offset", lambda: torch.FloatStorage(torch.FloatStorage(2), 4), "2", "4" 37) 38 39check_error( 40 "Negative offset", lambda: torch.FloatStorage(torch.FloatStorage(2), -1), "2", "-1" 41) 42 43check_error( 44 "Invalid size", 45 lambda: torch.FloatStorage(torch.FloatStorage(3), 1, 5), 46 "2", 47 "1", 48 "5", 49) 50 51check_error( 52 "Negative size", 53 lambda: torch.FloatStorage(torch.FloatStorage(3), 1, -5), 54 "2", 55 "1", 56 "-5", 57) 58 59check_error("Invalid index type", lambda: torch.FloatStorage(10)["first item"], "str") 60 61 62def assign(): 63 torch.FloatStorage(10)[1:-1] = "1" 64 65 66check_error("Invalid value type", assign, "str") 67 68check_error( 69 "resize_ with invalid type", lambda: torch.FloatStorage(10).resize_(1.5), "float" 70) 71 72check_error( 73 "fill_ with invalid type", lambda: torch.IntStorage(10).fill_("asdf"), "str" 74) 75 76# TODO: frombuffer 77