-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
148 lines (126 loc) · 4.08 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"Wordle unit tests"
import unittest
import gym
import numpy
from gym.envs.registration import register
from wordle import WordleEnv
register(
id="wordle-v0",
entry_point="wordle:WordleEnv",
)
class TestWordle(unittest.TestCase):
"Wordle Tests"
def setUp(self):
"Set up the test"
self.wordle = WordleEnv()
def test_convert_word(self):
"Test converting an ASCII word to an array of numbers to be used as an action"
word = "HELLO"
action = [7, 4, 11, 11, 14]
converted = WordleEnv.convert_word(word)
# print("word", word)
# print("action", action)
# print("converted", converted)
self.assertTrue(numpy.array_equal(action, converted))
def test_convert_action(self):
"Test converting an action array to a string"
word = "HELLO"
action = [7, 4, 11, 11, 14]
converted = WordleEnv.convert_action(action)
# print("word", word)
# print("action", action)
# print("converted", converted)
self.assertTrue(word == converted)
def test_check_letter(self):
"Test checking for misplaced letters"
letters = []
letters.append(
{
"letter": "A",
"seen": False,
"position": 0,
"b": 0,
}
)
letters.append(
{
"letter": "B",
"seen": False,
"position": 1,
"b": 1,
}
)
letters.append(
{
"letter": "C",
"seen": False,
"position": 2,
"b": 2,
}
)
letters.append(
{
"letter": "D",
"seen": False,
"position": 3,
"b": 3,
}
)
letters.append(
{
"letter": "E",
"seen": False,
"position": 4,
"b": 4,
}
)
self.assertEqual(True, WordleEnv.check_letter(1, letters))
self.assertEqual(False, WordleEnv.check_letter(1, letters))
self.assertEqual(False, WordleEnv.check_letter(20, letters))
def test_run(self):
"Test a few steps"
env = gym.make("wordle-v0")
observation = env.reset()
env.set_answer("WORLD")
action = WordleEnv.convert_word("HELLO")
observation, reward, done, _ = env.step(action)
# env.render()
self.assertEqual(observation[0], 0)
self.assertEqual(observation[1], 0)
self.assertEqual(observation[2], 0)
self.assertEqual(observation[3], 2)
self.assertEqual(observation[4], 1)
self.assertEqual(done, False)
env.set_answer("HELLO")
action = WordleEnv.convert_word("WORLD")
observation, reward, done, _ = env.step(action)
# env.render()
self.assertEqual(observation[0], 0)
self.assertEqual(observation[1], 1)
self.assertEqual(observation[2], 0)
self.assertEqual(observation[3], 2)
self.assertEqual(observation[4], 0)
self.assertEqual(done, False)
action = WordleEnv.convert_word("HWHWH")
observation, reward, done, _ = env.step(action)
# env.render()
self.assertEqual(observation[0], 0)
self.assertEqual(observation[1], 0)
self.assertEqual(observation[2], 0)
self.assertEqual(observation[3], 0)
self.assertEqual(observation[4], 0)
self.assertEqual(reward < 0, True)
self.assertEqual(done, False)
action = WordleEnv.convert_word("HELLO")
observation, reward, done, _ = env.step(action)
# env.render()
self.assertEqual(observation[0], 2)
self.assertEqual(observation[1], 2)
self.assertEqual(observation[2], 2)
self.assertEqual(observation[3], 2)
self.assertEqual(observation[4], 2)
self.assertEqual(reward > 0, True)
self.assertEqual(done, True)
env.close()
if __name__ == "__main__":
unittest.main()