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