27
27
import net .minecraft .enchantment .Enchantment ;
28
28
import net .minecraft .enchantment .EnchantmentData ;
29
29
import net .minecraft .init .Bootstrap ;
30
- import net .minecraft .item . Item ;
30
+ import net .minecraft .init . Items ;
31
31
import net .minecraft .item .ItemStack ;
32
32
33
33
public class EnchantmentWorkerTest {
@@ -44,6 +44,7 @@ public class EnchantmentWorkerTest {
44
44
}
45
45
}
46
46
47
+ // A generic test observation
47
48
private static Observation getTestObservation () {
48
49
Observation observation = new Observation ();
49
50
observation .truncatedSeed = 0x2340 ;
@@ -57,7 +58,26 @@ private static Observation getTestObservation() {
57
58
observation .enchantLevels [1 ] = 2 ;
58
59
observation .enchants [2 ] = 1 ; // Fire Protection
59
60
observation .enchantLevels [2 ] = 2 ;
60
- observation .item = new ItemStack (Item .getByNameOrId ("diamond_leggings" ));
61
+ observation .item = new ItemStack (Items .diamond_leggings );
62
+ return observation ;
63
+ }
64
+
65
+ // An observation at low power that has two empty slots, which has exposed bugs
66
+ // in the past.
67
+ private static Observation getWeakObservation () {
68
+ Observation observation = new Observation ();
69
+ observation .truncatedSeed = 0x08e0 ;
70
+ observation .power = 0 ;
71
+ observation .levels [0 ] = 1 ;
72
+ observation .levels [1 ] = 3 ;
73
+ observation .levels [2 ] = 4 ;
74
+ observation .enchants [0 ] = -1 ;
75
+ observation .enchantLevels [0 ] = 0 ;
76
+ observation .enchants [1 ] = -1 ;
77
+ observation .enchantLevels [1 ] = 0 ;
78
+ observation .enchants [2 ] = 0x22 ; // Unbreaking
79
+ observation .enchantLevels [2 ] = 1 ;
80
+ observation .item = new ItemStack (Items .fishing_rod );
61
81
return observation ;
62
82
}
63
83
@@ -77,34 +97,12 @@ public void testTestLevels() {
77
97
assertTrue (count >= 10000 );
78
98
}
79
99
80
- @ Test
81
- public void testTestEnchants () {
82
- Random rand = new Random (0 );
83
- Observation observation = getTestObservation ();
84
- ItemStack item = observation .item ;
85
- List <List <EnchantmentData >> cachedEnchantmentList = EnchantmentWorker .buildEnchantListCache (item );
100
+ private static EnchantmentWorker runWorkerLoop (Observation observation , String useSeed , int expectedCandidates ) throws InterruptedException {
86
101
Enchantment [] targets = new Enchantment [3 ];
87
102
for (int i = 0 ; i < 3 ; ++i ) {
88
103
targets [i ] = Enchantment .getEnchantmentByID (observation .enchants [i ]);
89
104
}
90
- int enchantability = item .getItem ().getItemEnchantability (item );
91
- @ SuppressWarnings ("unchecked" ) List <EnchantmentData >[] tempData = new List [3 ];
92
- int count = 0 ;
93
- for (int i = 0 ; i < 100000 ; ++i ) {
94
- boolean expected = EnchantmentWorker .testEnchants (rand , i , observation , tempData );
95
- boolean actual = EnchantmentWorker .testEnchantFast (rand , i , observation , false , cachedEnchantmentList , tempData , targets [2 ], enchantability , 2 ) && EnchantmentWorker .testEnchantFast (rand , i , observation , false , cachedEnchantmentList , tempData , targets [1 ], enchantability , 1 ) && EnchantmentWorker .testEnchantFast (rand , i , observation , false , cachedEnchantmentList , tempData , targets [0 ], enchantability , 0 );
96
- assertEquals (expected , actual );
97
- if (expected ) {
98
- count ++;
99
- }
100
- }
101
- assertTrue (count >= 100 );
102
- }
103
-
104
- @ Test
105
- public void testFullRun () throws InterruptedException {
106
- Observation observation = getTestObservation ();
107
- EnchantmentWorker worker = new EnchantmentWorker (/*useSeed=*/ "always" );
105
+ EnchantmentWorker worker = new EnchantmentWorker (useSeed );
108
106
worker .addObservation (observation );
109
107
// Wait for worker to finish
110
108
EnchantmentWorker .State state = worker .state ;
@@ -113,40 +111,72 @@ public void testFullRun() throws InterruptedException {
113
111
state = worker .state ;
114
112
}
115
113
assertEquals ("enchantmentrevealer.status.possibles" , state .statusMessage );
116
- assertEquals (worker .candidatesLength , state .counts [0 ][0 ]);
117
- assertEquals ("Found the wrong number of candidates!" , 12 , state .counts [0 ][0 ]);
114
+ assertEquals (worker .candidatesLength , state .counts [2 ][0 ]);
115
+ assertEquals ("Found the wrong number of candidates!" , expectedCandidates , state .counts [2 ][0 ]);
116
+ return worker ;
117
+ }
118
+
119
+ private static void runFastWorkerTest (Observation observation , int seed , int expectedCandidates )
120
+ throws InterruptedException {
121
+ EnchantmentWorker worker = runWorkerLoop (observation , "always" , expectedCandidates );
118
122
int i = 0 ;
119
- while (i < worker .candidatesLength && worker .candidates [i ] != 0x12347 ) {
123
+ while (i < worker .candidatesLength && worker .candidates [i ] != seed ) {
120
124
++i ;
121
125
}
122
126
assertNotEquals ("The correct seed was not among the candidates!" , worker .candidatesLength , i );
127
+ Random rand = new Random (0 );
128
+ ItemStack item = observation .item ;
129
+ List <List <EnchantmentData >> cachedEnchantmentList = EnchantmentWorker .buildEnchantListCache (item );
130
+ Enchantment [] targets = new Enchantment [3 ];
131
+ for (i = 0 ; i < 3 ; ++i ) {
132
+ targets [i ] = Enchantment .getEnchantmentByID (observation .enchants [i ]);
133
+ }
134
+ @ SuppressWarnings ("unchecked" ) List <EnchantmentData >[] tempEnchantmentData = new List [3 ];
135
+ int enchantability = item .getItem ().getItemEnchantability (item );
136
+ for (i = 0 ; i < worker .candidatesLength ; ++i ) {
137
+ for (int j = 0 ; j < 3 ; ++j ) {
138
+ assertTrue ("Failure for " + j + " at i=" + i ,
139
+ EnchantmentWorker .testEnchantFast (rand , worker .candidates [i ], observation , false ,
140
+ cachedEnchantmentList , tempEnchantmentData , targets [j ], enchantability , j ));
141
+ }
142
+ }
123
143
}
124
144
125
- // This test takes ~1 minute to run.
126
145
@ Test
127
- public void testFullFullRun () throws InterruptedException {
128
- Observation observation = getTestObservation ();
129
- EnchantmentWorker worker = new EnchantmentWorker ( /*useSeed=*/ "never" );
130
- worker . addObservation ( observation );
131
- // Wait for worker to finish
132
- EnchantmentWorker . State state = worker . state ;
133
- while ( state . enchants == EnchantmentWorker . NO_STRINGS ) {
134
- Thread . sleep ( 50 );
135
- state = worker . state ;
136
- }
137
- assertEquals ( "enchantmentrevealer.status.possibles" , state . statusMessage );
138
- assertEquals ( worker . candidatesLength , state . counts [ 0 ][ 0 ]);
139
- assertEquals ( "Found the wrong number of candidates!" , 38722 , state . counts [ 0 ][ 0 ] );
146
+ public void testFastFullRun () throws InterruptedException {
147
+ runFastWorkerTest ( getTestObservation (), 0x12347 , 12 );
148
+ }
149
+
150
+ @ Test
151
+ public void testFastWeakRun () throws InterruptedException {
152
+ runFastWorkerTest ( getWeakObservation (), 0x249e08e4 , 18202 );
153
+ }
154
+
155
+ // These tests take >1 minute to run.
156
+ private static void runSlowWorkerTest ( Observation observation , int seed , int expectedCandidates )
157
+ throws InterruptedException {
158
+ EnchantmentWorker worker = runWorkerLoop ( observation , "never" , expectedCandidates );
140
159
int i = 0 ;
141
- while (i < worker .candidatesLength && worker .candidates [i ] != 0x12347 ) {
160
+ while (i < worker .candidatesLength && worker .candidates [i ] != seed ) {
142
161
++i ;
143
162
}
144
163
assertNotEquals ("The correct seed was not among the candidates!" , worker .candidatesLength , i );
145
164
Random rand = new Random (0 );
146
165
@ SuppressWarnings ("unchecked" ) List <EnchantmentData >[] tempEnchantmentData = new List [3 ];
147
166
for (i = 0 ; i < worker .candidatesLength ; ++i ) {
148
167
assertTrue ("Failure at i=" + i ,
149
- EnchantmentWorker .testEnchants (rand , worker .candidates [i ], observation , tempEnchantmentData ));
168
+ EnchantmentWorker .testEnchants (
169
+ rand , worker .candidates [i ], observation , tempEnchantmentData ));
150
170
}
151
171
}
172
+
173
+ @ Test
174
+ public void testSlowFullRun () throws InterruptedException {
175
+ runSlowWorkerTest (getTestObservation (), 0x12347 , 38722 );
176
+ }
177
+
178
+ @ Test
179
+ public void testSlowWeakRun () throws InterruptedException {
180
+ runSlowWorkerTest (getWeakObservation (), 0x249e08e4 , 75093865 );
181
+ }
152
182
}
0 commit comments