java - Random strings with given length unit testing -


i have program generating pseudo random numbers(only lowercase, uppercase , digits allowed).

/**  *   * @return - returns random digit (from 0 9)  *   */ int randomdigits() {      return (int) (math.random() * 10); }  /**  *   * @return - returns random lowercase (from "a" "z")  */ char randomlowercase() {      return (char) ('a' + math.random() * 26); }  /**  *   * @return - returns random uppercase (from "a" "z")  */ char randomuppercase() {      return (char) ('a' + math.random() * 26); }  /**  *   * @return - returns random number between 1 , 3.  *   */ char randomchoice() {      return (char) ((char) (math.random() * 3) + 1); }  /**  *   * @param length  *            - length of random string.  * @return - returns combined random string. elements builder side  *         side.  *   *         use randomchoice method randomly upper, lower ,  *         digits.  */ public string stringbuilder(int length) {     stringbuilder result = new stringbuilder();     int len = length;      (int = 0; < len; i++) {         int ch = randomchoice();         if (ch == 1) {             result.append(randomdigits());         }         if (ch == 2) {             result.append(randomlowercase());         }         if (ch == 3) {             result.append(randomuppercase());          }     }     return result.tostring();  } 

how can make test code. try test range digits (form 0 9)

    int minrange = 0;     int maxrange = 0;      (int = 0; < 100000; i++) {         int result = item.randomdigits();         if (result == 52) {             minrange++;          } else {             if (result == 19) {                 maxrange++;              }         }     }     logger.info("the min range in digit 0, , in test appeared: {}", minrange);     logger.info("the max range in digit 9, , in test appeared: {}", maxrange); 

but cant find how test lower or upper?

testing code uses randomness tricky. there 2 approaches can take:

  1. your test can have sufficient iterations has chance show errors in logic. many cases iterating 1000 or 1000000 times , checking consistency of answers reasonable. option if looking check required distribution across range.

this might like:

for (int = 0; < 1000000; i++)     asserttrue(isvalid(new randomval())); 

if want check characters appear @ least once:

assertequals(26 * 2 + 9, intstream.range(0, 1000000)     .maptoobj(n -> stringbuilder(6))     .flatmap(string::chars)     .collect(collectors.toset())     .size()); 

this uses java 8 , adds every character (as integer) set , checks how large afterwards.

  1. using mocking framework (such mockito) check result expected 1 specific outputs whatever using generate randomness. best way test correct result boundary conditions (i.e. generator returning results @ each end of range).

this might like:

random mockrandom = mock(random.class); when(mockrandom.nextfloat()).thenreturn(0.0f); asserttrue(isvalid(new randomval(mockrandom)); when(mockrandom.nextfloat()).thenreturn(1.0f - float.min_value); asserttrue(isvalid(new randomval(mockrandom)); 

for completeness it's worth doing both of these.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -