Coverage for tests\unit\maze_dataset\generation\test_coord_str_tuple.py: 100%

73 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-23 12:49 -0700

1import numpy as np 

2import pytest 

3 

4from maze_dataset.token_utils import ( 

5 _coord_to_strings_indexed, 

6 _coord_to_strings_UT, 

7 coord_str_to_coord_np, 

8 coord_str_to_tuple, 

9 coord_str_to_tuple_noneable, 

10 coords_to_strings, 

11 str_is_coord, 

12) 

13 

14 

15def test_coord_to_strings(): 

16 assert _coord_to_strings_UT((1, 2)) == ["(1,2)"] 

17 # assert _coord_to_strings_UT((-1, 0)) == ["(-1,0)"] 

18 

19 assert _coord_to_strings_indexed((1, 2)) == ["(", "1", ",", "2", ")"] 

20 assert _coord_to_strings_indexed((-1, 0)) == ["(", "-1", ",", "0", ")"] 

21 

22 

23# TODO: test for negative coords 

24 

25 

26def test_str_is_coord(): 

27 assert str_is_coord("(1,2)") 

28 # assert str_is_coord("(-1,0)") 

29 assert str_is_coord("(1,2,3)") 

30 assert not str_is_coord("1,2") 

31 assert str_is_coord("(1, 2)") 

32 assert str_is_coord("( 1 , 2 )") 

33 assert not str_is_coord("(1, 2)", allow_whitespace=False) 

34 

35 

36def test_coord_str_to_tuple(): 

37 assert coord_str_to_tuple("(1,2)") == (1, 2) 

38 # assert coord_str_to_tuple("(-1,0)") == (-1, 0) 

39 assert coord_str_to_tuple("(1,2,3)") == (1, 2, 3) 

40 assert coord_str_to_tuple("(1, 2)") == (1, 2) 

41 assert coord_str_to_tuple("( 1 , 2 )") == (1, 2) 

42 assert coord_str_to_tuple("(1, 2)", allow_whitespace=False) == (1, 2) 

43 

44 

45def test_coord_str_to_coord_np(): 

46 assert (coord_str_to_coord_np("(1,2)") == np.array([1, 2])).all() 

47 # assert (coord_str_to_coord_np("(-1,0)") == np.array([-1, 0])).all() 

48 assert (coord_str_to_coord_np("(1,2,3)") == np.array([1, 2, 3])).all() 

49 assert (coord_str_to_coord_np("(1, 2)") == np.array([1, 2])).all() 

50 assert (coord_str_to_coord_np("( 1 , 2 )") == np.array([1, 2])).all() 

51 assert ( 

52 coord_str_to_coord_np("(1, 2)", allow_whitespace=False) == np.array([1, 2]) 

53 ).all() 

54 

55 

56def test_coord_str_to_tuple_noneable(): 

57 assert coord_str_to_tuple_noneable("(1,2)") == (1, 2) 

58 # assert coord_str_to_tuple_noneable("(-1,0)") == (-1, 0) 

59 assert coord_str_to_tuple_noneable("(1,2,3)") == (1, 2, 3) 

60 assert coord_str_to_tuple_noneable("(1, 2)") == (1, 2) 

61 assert coord_str_to_tuple_noneable("( 1 , 2 )") == (1, 2) 

62 assert coord_str_to_tuple_noneable("1,2") is None 

63 

64 

65def test_coords_to_strings(): 

66 # TODO: resolve testing duplication in test_token_utils.py 

67 assert coords_to_strings( 

68 [(1, 2), "<ADJLIST_START>", (5, 6)], _coord_to_strings_UT 

69 ) == ["(1,2)", "(5,6)"] 

70 assert coords_to_strings( 

71 [(1, 2), "<ADJLIST_START>", (5, 6)], _coord_to_strings_UT, when_noncoord="skip" 

72 ) == ["(1,2)", "(5,6)"] 

73 assert coords_to_strings( 

74 [(1, 2), "<ADJLIST_START>", (5, 6)], 

75 _coord_to_strings_UT, 

76 when_noncoord="include", 

77 ) == ["(1,2)", "<ADJLIST_START>", "(5,6)"] 

78 with pytest.raises(ValueError): 

79 coords_to_strings( 

80 [(1, 2), "<ADJLIST_START>", (5, 6)], 

81 _coord_to_strings_UT, 

82 when_noncoord="error", 

83 ) 

84 

85 assert coords_to_strings( 

86 [(1, 2), "<ADJLIST_START>", (5, 6)], _coord_to_strings_indexed 

87 ) == ["(", "1", ",", "2", ")", "(", "5", ",", "6", ")"] 

88 assert coords_to_strings( 

89 [(1, 2), "<ADJLIST_START>", (5, 6)], 

90 _coord_to_strings_indexed, 

91 when_noncoord="skip", 

92 ) == ["(", "1", ",", "2", ")", "(", "5", ",", "6", ")"] 

93 assert coords_to_strings( 

94 [(1, 2), "<ADJLIST_START>", (5, 6)], 

95 _coord_to_strings_indexed, 

96 when_noncoord="include", 

97 ) == ["(", "1", ",", "2", ")", "<ADJLIST_START>", "(", "5", ",", "6", ")"] 

98 

99 

100def test_str_is_coord_2(): 

101 assert str_is_coord("(1,2)") 

102 assert str_is_coord("( 1 , 2 )") 

103 assert not str_is_coord("1,2") 

104 assert not str_is_coord("(1,2") 

105 assert not str_is_coord("1,2)") 

106 assert not str_is_coord("(1, a)") 

107 assert not str_is_coord("()") 

108 

109 

110def test_coord_str_to_tuple_excepts(): 

111 assert coord_str_to_tuple("(1,2)") == (1, 2) 

112 with pytest.raises(ValueError): 

113 coord_str_to_tuple("(1, a)") 

114 with pytest.raises(ValueError): 

115 coord_str_to_tuple("()") 

116 

117 

118def test_coord_str_to_tuple_noneable_2(): 

119 assert coord_str_to_tuple_noneable("(1,2)") == (1, 2) 

120 assert coord_str_to_tuple_noneable("1,2") is None 

121 assert coord_str_to_tuple_noneable("(1,2") is None 

122 assert coord_str_to_tuple_noneable("1,2)") is None 

123 assert coord_str_to_tuple_noneable("(1, a)") is None 

124 assert coord_str_to_tuple_noneable("()") is None 

125 

126 

127def test_coord_to_str(): 

128 assert _coord_to_strings_UT((1, 2)) == ["(1,2)"] 

129 assert _coord_to_strings_UT((10, 20)) == ["(10,20)"] 

130 assert _coord_to_strings_UT((0, 0)) == ["(0,0)"] 

131 with pytest.raises(TypeError): 

132 _coord_to_strings_UT(1) 

133 

134 assert _coord_to_strings_indexed((1, 2)) == ["(", "1", ",", "2", ")"] 

135 assert _coord_to_strings_indexed((10, 20)) == ["(", "10", ",", "20", ")"] 

136 assert _coord_to_strings_indexed((0, 0)) == ["(", "0", ",", "0", ")"] 

137 with pytest.raises(TypeError): 

138 _coord_to_strings_indexed(1)