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
- */
- public class StaticMethodTest extends TestCase {
- private static String getPercentageRounds(final double percentage) {
- return getPercentageRounds_JeffGrigg(percentage);
- // return getPercentageRounds_AllenHolub(percentage);
- // return getPercentageRounds_Mix(percentage);
- }
- private static String getPercentageRounds_JeffGrigg(final double percentage) {
- final var blueCircles = max(min((int) ceil(percentage * 10), 10), 0);
- final var emptyCircles = 10 - blueCircles;
- return "π΅".repeat(blueCircles) + "βͺ".repeat(emptyCircles);
- }
- private static String getPercentageRounds_AllenHolub(final double percentage) {
- assert 0.0 <= percentage && percentage <= 1.0;
- final String[] dots = new String[]{
- "βͺβͺβͺβͺβͺβͺβͺβͺβͺβͺ",
- "π΅βͺβͺβͺβͺβͺβͺβͺβͺβͺ",
- "π΅π΅βͺβͺβͺβͺβͺβͺβͺβͺ",
- "π΅π΅π΅βͺβͺβͺβͺβͺβͺβͺ",
- "π΅π΅π΅π΅βͺβͺβͺβͺβͺβͺ",
- "π΅π΅π΅π΅π΅βͺβͺβͺβͺβͺ",
- "π΅π΅π΅π΅π΅π΅βͺβͺβͺβͺ",
- "π΅π΅π΅π΅π΅π΅π΅βͺβͺβͺ",
- "π΅π΅π΅π΅π΅π΅π΅π΅βͺβͺ",
- "π΅π΅π΅π΅π΅π΅π΅π΅π΅βͺ",
- "π΅π΅π΅π΅π΅π΅π΅π΅π΅π΅"
- };
- final var percentageAsIntBetween0And10 = (int) (percentage * 10);
- return dots[percentageAsIntBetween0And10];
- }
- private static String getPercentageRounds_Mix(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