Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import static java.lang.Math.*;
- /**
- * Inspired by twitter post https://twitter.com/allenholub/status/1617619350080598016
- * And https://twitter.com/lucasmeijer/status/1615767525928615956
- */
- public class StaticMethodTest extends TestCase {
- private static String getPercentageRounds(final double percentage) {
- return getPercentageRounds_JeffGrigg(percentage);
- // return getPercentageRounds_AllenHolub(percentage);
- // return getPercentageRounds_AllenHolub_JeffGrigg(percentage);
- // return getPercentageRounds_MichaelGrafl(percentage);
- // return getPercentageRounds_NSS(percentage);
- }
- private static final int TOTAL_NUMBER_OF_DOTS = 10;
- private static String getPercentageRounds_JeffGrigg(final double percentage) {
- final var blueCircles = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
- final var emptyCircles = TOTAL_NUMBER_OF_DOTS - blueCircles;
- return "🔵".repeat(blueCircles) + "⚪".repeat(emptyCircles);
- }
- private static final String[] _dots = new String[]{
- "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"
- };
- private static String getPercentageRounds_AllenHolub(final double percentage) {
- // assert 0.0 <= percentage && percentage <= 1.0;
- final var percentageAsIntBetween0And10 = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
- // final var percentageAsIntBetween0And10 = (int) (percentage * 10);
- return _dots[percentageAsIntBetween0And10];
- }
- private static String getPercentageRounds_MichaelGrafl(final double percentage) {
- final var pot = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
- // final var pot = (int) (percentage * 10 + 0.5);
- switch (pot) {
- case 0: return "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪";
- case 1: return "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪";
- case 2: return "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪";
- case 3: return "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪";
- case 4: return "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪";
- case 5: return "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪";
- case 6: return "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪";
- case 7: return "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪";
- case 8: return "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪";
- case 9: return "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪";
- case 10: return "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵";
- default: return "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪";
- }
- }
- // https://twitter.com/NabheetS/status/1616461311139016710
- private static String getPercentageRounds_NSS(final double percentage) {
- final var blueCircles = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
- final var result = new StringBuilder(10);
- for (int i = 0; i < 10; ++i) {
- result.append(i < blueCircles ? "🔵" : "⚪");
- }
- return result.toString();
- }
- private static String getPercentageRounds_AllenHolub_JeffGrigg(final double percentage) {
- final String[] dots = new String[]{
- "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪",
- "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"
- };
- final var percentageAsIntBetween0And10 = max(min((int) ceil(percentage * 10), 10), 0);
- return dots[percentageAsIntBetween0And10];
- }
- public void testZero() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.00));
- }
- public void testJustOverZero() {
- assertEquals("🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.00001));
- }
- public void test10Percent() {
- assertEquals("🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.10));
- }
- public void testJustOver10Percent() {
- assertEquals("🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.100001));
- }
- public void test20Percent() {
- assertEquals("🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.20));
- }
- public void testJustOver20Percent() {
- assertEquals("🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.200001));
- }
- public void test30Percent() {
- assertEquals("🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.30));
- }
- public void testJustOver30Percent() {
- assertEquals("🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.300001));
- }
- public void test40Percent() {
- assertEquals("🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.40));
- }
- public void testJustOver40Percent() {
- assertEquals("🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪", getPercentageRounds(0.400001));
- }
- public void test50Percent() {
- assertEquals("🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪", getPercentageRounds(0.50));
- }
- public void testJustOver50Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪", getPercentageRounds(0.500001));
- }
- public void test60Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪", getPercentageRounds(0.60));
- }
- public void testJustOver60Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪", getPercentageRounds(0.600001));
- }
- public void test70Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪", getPercentageRounds(0.70));
- }
- public void testJustOver70Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪", getPercentageRounds(0.700001));
- }
- public void test80Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪", getPercentageRounds(0.80));
- }
- public void testJustOver80Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪", getPercentageRounds(0.800001));
- }
- public void test90Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪", getPercentageRounds(0.90));
- }
- public void testJustOver90Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(0.900001));
- }
- public void testAlmost100Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(0.999999));
- }
- public void test100Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(1.00));
- }
- public void testJustOver100Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(1.0000001));
- }
- public void test200Percent() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(2.00));
- }
- public void testCrazyLargePercentage() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(999999.999999));
- }
- public void testMaxIntegerValue() {
- assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(Integer.MAX_VALUE));
- }
- public void testJustBelowZero() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.0000001));
- }
- public void testNegative10Percent() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.10));
- }
- public void testNegative20Percent() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.20));
- }
- public void testCrazyLargeNegativePercentage() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-999999.999999));
- }
- public void testMinIntegerValue() {
- assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(Integer.MIN_VALUE));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement