Last active
April 30, 2022 16:00
-
-
Save kitsook/e273644a0d7ab7a956e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.clarenceho.test; | |
public class FizzBuzz { | |
public static String doFizzBuzz1(int i) { | |
StringBuilder sb = new StringBuilder(); | |
if (i % 3 == 0) { | |
sb.append("Fizz"); | |
} | |
if (i % 5 == 0) { | |
sb.append("Buzz"); | |
} | |
if (sb.length() == 0) { | |
sb.append(i); | |
} | |
return sb.toString(); | |
} | |
public static String doFizzBuzz2(int i) { | |
if (i % 3 == 0) { | |
if (i % 5 == 0) { | |
return "FizzBuzz"; | |
} else { | |
return "Fizz"; | |
} | |
} else if (i % 5 == 0) { | |
return "Buzz"; | |
} | |
return String.valueOf(i); | |
} | |
public static String doFizzBuzz3(int i) { | |
if (i % 15 == 0) { | |
return "FizzBuzz"; | |
} else if (i % 5 == 0) { | |
return "Buzz"; | |
} else if (i % 3 == 0) { | |
return "Fizz"; | |
} | |
return String.valueOf(i); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.clarenceho.test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.stream.IntStream; | |
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.FixMethodOrder; | |
import org.junit.Test; | |
import org.junit.runners.MethodSorters; | |
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | |
public class TestFizzBuzz { | |
String expected[] = { | |
"FizzBuzz", "1", "2", "Fizz", "4", "Buzz", | |
"Fizz", "7", "8", "Fizz", "Buzz", | |
"11", "Fizz", "13", "14", "FizzBuzz", | |
"16", "17", "Fizz", "19", "Buzz", | |
"Fizz", "22", "23", "Fizz", "Buzz", | |
"26", "Fizz", "28", "29", "FizzBuzz", | |
}; | |
// list of FizzBuzz methods to test | |
List<Function<Integer, String>> methods = new ArrayList<>(); | |
@Before | |
public void setup() { | |
methods.add(FizzBuzz::doFizzBuzz1); | |
methods.add(FizzBuzz::doFizzBuzz2); | |
methods.add(FizzBuzz::doFizzBuzz3); | |
} | |
@Test | |
public void doSample() { | |
for (Function<Integer, String> m: methods) { | |
for (int i = 0; i < expected.length; i++) { | |
assertEquals(expected[i], m.apply(i)); | |
} | |
} | |
} | |
@Test | |
public void testMaxRange() { | |
for (Function<Integer, String> m: methods) { | |
assertTrue(autoTest(m, Integer.MIN_VALUE, Integer.MAX_VALUE)); | |
} | |
} | |
@Test | |
public void timeAll() { | |
for (Function<Integer, String> m: methods) { | |
long start = System.nanoTime(); | |
autoTest(m, Integer.MIN_VALUE, Integer.MAX_VALUE); | |
long end = System.nanoTime(); | |
System.err.println("Time taken to run " + m.toString() + " is " + ((end - start) / 1000000000.0) +"s"); | |
} | |
} | |
private boolean autoTest(Function<Integer, String> opr, int start, int end) { | |
return IntStream | |
.range(start, end) | |
.parallel() | |
.map(i -> { | |
String r = opr.apply(i); | |
if (i % 15 == 0 && "FizzBuzz".equals(r)) { | |
return 0; | |
} else if (i % 3 == 0 && "Fizz".equals(r)) { | |
return 0; | |
} else if (i % 5 == 0 && "Buzz".equals(r)) { | |
return 0; | |
} else if (String.valueOf(i).equals(r)) { | |
return 0; | |
} | |
return 1; | |
}) | |
.filter(i -> i != 0) | |
.findFirst() | |
.isPresent() == false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment