Advertisement
JeffGrigg

Untitled

Jan 23rd, 2023
2,841
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