@@ -151,10 +151,7 @@ class ProgramBuilderTests: XCTestCase {
151151 // This testcase demonstrates the behavior of `b.randomVariable(forUseAs:)`
152152 // This API behaves in the following way:
153153 // - It prefers to return variables that are known to have the requested type
154- // with probability `b.probabilityOfVariableSelectionTryingToFindAnExactMatch`,
155- // but only if there's a sufficient number of them currently visible (to ensure
156- // that consecutive queries return different variables. This threshold is
157- // determined by `b.minVisibleVariablesOfRequestedTypeForVariableSelection`.
154+ // with probability `b.probabilityOfVariableSelectionTryingToFindAnExactMatch`.
158155 // - Otherwise, it tries a wider match, including all variables that may have the
159156 // requested type. This includes all variables that have unknown type.
160157 // - If even that doesn't find any matches, the function will return a random
@@ -165,8 +162,6 @@ class ProgramBuilderTests: XCTestCase {
165162 let b = fuzzer. makeBuilder ( )
166163
167164 let i = b. loadInt ( 42 )
168- // There is only one visible variable, so we always get that, no matter what we actually query
169- XCTAssertLessThan ( b. numberOfVisibleVariables, b. minVisibleVariablesOfRequestedTypeForVariableSelection)
170165 XCTAssertEqual ( b. randomVariable ( forUseAs: . integer) , i)
171166 XCTAssertEqual ( b. randomVariable ( forUseAs: . jsAnything) , i)
172167 XCTAssertEqual ( b. randomVariable ( forUseAs: . string) , i)
@@ -178,31 +173,24 @@ class ProgramBuilderTests: XCTestCase {
178173 XCTAssert ( [ i, s] . contains ( b. randomVariable ( forUseAs: . primitive) ) )
179174 XCTAssertEqual ( b. randomVariable ( forUseAs: . string) , s)
180175
181- // Now there's also a variable of unknown type, which may be anything. Since we don't have enough variables
182- // of a known type, all queries will use a `MayBe` type query to find matches and so may return the unknown variable.
176+ // Now there's also a variable of unknown type, which may be anything.
183177 let unknown = b. createNamedVariable ( forBuiltin: " unknown " )
184178 XCTAssertEqual ( b. type ( of: unknown) , . jsAnything)
185179
180+ // There is now still a 50% chance that we will do a `MayBe` query, so we may return the unknown variable.
186181 XCTAssert ( [ i, unknown] . contains ( b. randomVariable ( forUseAs: . integer) ) )
187182 XCTAssert ( [ i, unknown] . contains ( b. randomVariable ( forUseAs: . number) ) )
188183 XCTAssert ( [ s, unknown] . contains ( b. randomVariable ( forUseAs: . string) ) )
189184 XCTAssert ( [ i, s, unknown] . contains ( b. randomVariable ( forUseAs: . primitive) ) )
190185 XCTAssert ( [ i, s, unknown] . contains ( b. randomVariable ( forUseAs: . jsAnything) ) )
191186
192- // Now we add some more integers and set the probability of trying an exact match (i.e. an `Is` query instead of a
193- // `MayBe` query) to 100%. Then, we expect to always get back the known integers when asking for them.
194- let i2 = b. loadInt ( 43 )
195- let i3 = b. loadInt ( 44 )
196- XCTAssertGreaterThanOrEqual ( b. numberOfVisibleVariables, b. minVisibleVariablesOfRequestedTypeForVariableSelection)
197187 b. probabilityOfVariableSelectionTryingToFindAnExactMatch = 1.0
198- // Now we should always get back the integer when querying for that type.
199- XCTAssert ( [ i, i2, i3] . contains ( b. randomVariable ( forUseAs: . integer) ) )
200- XCTAssert ( [ i, i2, i3] . contains ( b. randomVariable ( forUseAs: . number) ) )
201- // We don't have enough strings yet though.
202- XCTAssert ( [ s, unknown] . contains ( b. randomVariable ( forUseAs: . string) ) )
203- // But enough primitive values.
204- XCTAssert ( [ i, i2, i3, s] . contains ( b. randomVariable ( forUseAs: . primitive) ) )
205- XCTAssert ( [ i, i2, i3, s, unknown] . contains ( b. randomVariable ( forUseAs: . jsAnything) ) )
188+ // We should now always first look for a variable whose type matches or subsumes the requested one.
189+ XCTAssertEqual ( b. randomVariable ( ofType: . integer) , i)
190+ XCTAssertEqual ( b. randomVariable ( ofType: . number) , i)
191+ XCTAssertEqual ( b. randomVariable ( ofType: . string) , s)
192+ XCTAssert ( [ i, s] . contains ( b. randomVariable ( forUseAs: . primitive) ) )
193+ XCTAssert ( [ i, s, unknown] . contains ( b. randomVariable ( forUseAs: . jsAnything) ) )
206194 }
207195
208196 func testVariableRetrieval2( ) {
0 commit comments