Advertisement
JeffGrigg

Untitled

Jan 23rd, 2023
2,760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.85 KB | Source Code | 0 0
  1. import junit.framework.TestCase;
  2. import static java.lang.Math.*;
  3.  
  4. /**
  5.  * Inspired by twitter post https://twitter.com/allenholub/status/1617619350080598016
  6.  */
  7. public class StaticMethodTest extends TestCase {
  8.  
  9.     private static String getPercentageRounds(final double percentage) {
  10.         return getPercentageRounds_JeffGrigg(percentage);
  11. //        return getPercentageRounds_AllenHolub(percentage);
  12. //        return getPercentageRounds_Mix(percentage);
  13.     }
  14.  
  15.     private static String getPercentageRounds_JeffGrigg(final double percentage) {
  16.         final var blueCircles = max(min((int) ceil(percentage * 10), 10), 0);
  17.         final var emptyCircles = 10 - blueCircles;
  18.         return "🔵".repeat(blueCircles) + "⚪".repeat(emptyCircles);
  19.     }
  20.  
  21.     private static String getPercentageRounds_AllenHolub(final double percentage) {
  22.         assert 0.0 <= percentage && percentage <= 1.0;
  23.         final String[] dots = new String[]{
  24.                 "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪",
  25.                 "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪",
  26.                 "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪",
  27.                 "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪",
  28.                 "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪",
  29.                 "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪",
  30.                 "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪",
  31.                 "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪",
  32.                 "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪",
  33.                 "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪",
  34.                 "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"
  35.         };
  36.         final var percentageAsIntBetween0And10 = (int) (percentage * 10);
  37.         return dots[percentageAsIntBetween0And10];
  38.     }
  39.  
  40.     private static String getPercentageRounds_Mix(final double percentage) {
  41.         final String[] dots = new String[]{
  42.                 "⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪",
  43.                 "🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪",
  44.                 "🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪",
  45.                 "🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪",
  46.                 "🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪",
  47.                 "🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪",
  48.                 "🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪",
  49.                 "🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪",
  50.                 "🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪",
  51.                 "🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪",
  52.                 "🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"
  53.         };
  54.         final var percentageAsIntBetween0And10 = max(min((int) ceil(percentage * 10), 10), 0);
  55.         return dots[percentageAsIntBetween0And10];
  56.     }
  57.  
  58.     public void testZero() {
  59.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.00));
  60.     }
  61.  
  62.     public void testJustOverZero() {
  63.         assertEquals("🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.00001));
  64.     }
  65.  
  66.     public void test10Percent() {
  67.         assertEquals("🔵⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.10));
  68.     }
  69.  
  70.     public void testJustOver10Percent() {
  71.         assertEquals("🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.100001));
  72.     }
  73.  
  74.     public void test20Percent() {
  75.         assertEquals("🔵🔵⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.20));
  76.     }
  77.  
  78.     public void testJustOver20Percent() {
  79.         assertEquals("🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.200001));
  80.     }
  81.  
  82.     public void test30Percent() {
  83.         assertEquals("🔵🔵🔵⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.30));
  84.     }
  85.  
  86.     public void testJustOver30Percent() {
  87.         assertEquals("🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.300001));
  88.     }
  89.  
  90.     public void test40Percent() {
  91.         assertEquals("🔵🔵🔵🔵⚪⚪⚪⚪⚪⚪", getPercentageRounds(0.40));
  92.     }
  93.  
  94.     public void testJustOver40Percent() {
  95.         assertEquals("🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪", getPercentageRounds(0.400001));
  96.     }
  97.  
  98.     public void test50Percent() {
  99.         assertEquals("🔵🔵🔵🔵🔵⚪⚪⚪⚪⚪", getPercentageRounds(0.50));
  100.     }
  101.  
  102.     public void testJustOver50Percent() {
  103.         assertEquals("🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪", getPercentageRounds(0.500001));
  104.     }
  105.  
  106.     public void test60Percent() {
  107.         assertEquals("🔵🔵🔵🔵🔵🔵⚪⚪⚪⚪", getPercentageRounds(0.60));
  108.     }
  109.  
  110.     public void testJustOver60Percent() {
  111.         assertEquals("🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪", getPercentageRounds(0.600001));
  112.     }
  113.  
  114.     public void test70Percent() {
  115.         assertEquals("🔵🔵🔵🔵🔵🔵🔵⚪⚪⚪", getPercentageRounds(0.70));
  116.     }
  117.  
  118.     public void testJustOver70Percent() {
  119.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪", getPercentageRounds(0.700001));
  120.     }
  121.  
  122.     public void test80Percent() {
  123.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵⚪⚪", getPercentageRounds(0.80));
  124.     }
  125.  
  126.     public void testJustOver80Percent() {
  127.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪", getPercentageRounds(0.800001));
  128.     }
  129.  
  130.     public void test90Percent() {
  131.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵⚪", getPercentageRounds(0.90));
  132.     }
  133.  
  134.     public void testJustOver90Percent() {
  135.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(0.900001));
  136.     }
  137.  
  138.     public void testAlmost100Percent() {
  139.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(0.999999));
  140.     }
  141.  
  142.     public void test100Percent() {
  143.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(1.00));
  144.     }
  145.  
  146.     public void testJustOver100Percent() {
  147.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(1.0000001));
  148.     }
  149.  
  150.     public void test200Percent() {
  151.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(2.00));
  152.     }
  153.  
  154.     public void testCrazyLargePercentage() {
  155.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(999999.999999));
  156.     }
  157.  
  158.     public void testMaxIntegerValue() {
  159.         assertEquals("🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵", getPercentageRounds(Integer.MAX_VALUE));
  160.     }
  161.  
  162.     public void testJustBelowZero() {
  163.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.0000001));
  164.     }
  165.  
  166.     public void testNegative10Percent() {
  167.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.10));
  168.     }
  169.  
  170.     public void testNegative20Percent() {
  171.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-0.20));
  172.     }
  173.  
  174.     public void testCrazyLargeNegativePercentage() {
  175.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(-999999.999999));
  176.     }
  177.  
  178.     public void testMinIntegerValue() {
  179.         assertEquals("⚪⚪⚪⚪⚪⚪⚪⚪⚪⚪", getPercentageRounds(Integer.MIN_VALUE));
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement