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