RFR: 8346107: Generators: testing utility for random value generation [v2]

Emanuel Peter epeter at openjdk.org
Wed Dec 18 12:49:37 UTC 2024


On Wed, 18 Dec 2024 11:54:09 GMT, theoweidmannoracle <duke at openjdk.org> wrote:

>> To be more clear, without generics:
>> 
>> 
>> abstract class FloatingGenerator {
>>       // specialCountDown detemines in how many iterations we generate the next special value.
>>      private final int specialMinFrequency;
>>      private final int specialMaxFrequency;
>>      private int specialCountDown;
>> 
>>     // .. constructor ...
>> 
>>      protected final bool shouldUseSpecial() {
>>          specialCountDown--;
>>          if (specialCountDown <= 0) {
>>              specialCountDown = RANDOM.nextInt(specialMinFrequency, specialMaxFrequency);
>>              return true;
>>          } else {
>>              return false;
>>          }  
>>      }
>> }
>> 
>> 
>> Then in SpecialDoubleGenerator:
>> 
>> 
>>      @Override
>>      public double nextDouble() {
>>          if (shouldUseSpecial()) {
>>              int r = RANDOM.nextInt(VALUES.length);
>>              return VALUES[r];
>>          } else {
>>              return backgroundGenerator.nextDouble();
>>          }
>>      }
>> 
>> 
>> If you make FloatingGenerator generic, though, of course, and rename the methods from nextDouble and nextFloat to just next for the generators, then everything except for the list of special values itself could be moved into FloatingGenerator. That would result in very clean code in my opinion. If you need to adjust the logic for special selection later, with the current approach, that requires changes in multiple locations, which in my experience, ultimately leads to inconsistencies.
>
> I posted my second comment without seeing your latest comment. So it's not really a reply to that.

Hmm. I would have to make the classes `FloatGenerator` and `DoubleGenerator` interfaces. But having default methods for interfaces is not really the recommended way, as far as I know. And then I cannot make those default methods final, which sucks - java does not allow it.

Alternative: I make a separate class `RandomDistance` to compose into the others, that one can query with `isDistanceReached`. Would that be worth it you think?

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/22716#discussion_r1890181242


More information about the hotspot-compiler-dev mailing list