Advertisement
JeffGrigg

Untitled

Jan 23rd, 2023
3,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 8.80 KB | None | 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.  * And https://twitter.com/lucasmeijer/status/1615767525928615956
  7.  */
  8. public class StaticMethodTest extends TestCase {
  9.  
  10.     private static String getPercentageRounds(final double percentage) {
  11.         return getPercentageRounds_JeffGrigg(percentage);
  12. //        return getPercentageRounds_AllenHolub(percentage);
  13. //        return getPercentageRounds_AllenHolub_JeffGrigg(percentage);
  14. //        return getPercentageRounds_MichaelGrafl(percentage);
  15. //        return getPercentageRounds_NSS(percentage);
  16.     }
  17.  
  18.     private static final int TOTAL_NUMBER_OF_DOTS = 10;
  19.  
  20.     private static String getPercentageRounds_JeffGrigg(final double percentage) {
  21.         final var blueCircles = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
  22.         final var emptyCircles = TOTAL_NUMBER_OF_DOTS - blueCircles;
  23.         return "πŸ”΅".repeat(blueCircles) + "βšͺ".repeat(emptyCircles);
  24.     }
  25.  
  26.     private static final String[] _dots = new String[]{
  27.             "βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  28.             "πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  29.             "πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  30.             "πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  31.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺ",
  32.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺ",
  33.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺ",
  34.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺ",
  35.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ",
  36.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ",
  37.             "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅"
  38.     };
  39.  
  40.     private static String getPercentageRounds_AllenHolub(final double percentage) {
  41. //        assert 0.0 <= percentage && percentage <= 1.0;
  42.         final var percentageAsIntBetween0And10 = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
  43. //        final var percentageAsIntBetween0And10 = (int) (percentage * 10);
  44.         return _dots[percentageAsIntBetween0And10];
  45.     }
  46.  
  47.     private static String getPercentageRounds_MichaelGrafl(final double percentage) {
  48.         final var pot = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
  49. //        final var pot = (int) (percentage * 10 + 0.5);
  50.         switch (pot) {
  51.             case 0: return "βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ";
  52.             case 1: return "πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ";
  53.             case 2: return "πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ";
  54.             case 3: return "πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺ";
  55.             case 4: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺ";
  56.             case 5: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺ";
  57.             case 6: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺ";
  58.             case 7: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺ";
  59.             case 8: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ";
  60.             case 9: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ";
  61.             case 10: return "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅";
  62.             default: return "βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ";
  63.         }
  64.     }
  65.  
  66.     // https://twitter.com/NabheetS/status/1616461311139016710
  67.     private static String getPercentageRounds_NSS(final double percentage) {
  68.         final var blueCircles = max(min((int) ceil(percentage * TOTAL_NUMBER_OF_DOTS), TOTAL_NUMBER_OF_DOTS), 0);
  69.         final var result = new StringBuilder(10);
  70.         for (int i = 0; i < 10; ++i) {
  71.             result.append(i < blueCircles ? "πŸ”΅" : "βšͺ");
  72.         }
  73.         return result.toString();
  74.     }
  75.  
  76.     private static String getPercentageRounds_AllenHolub_JeffGrigg(final double percentage) {
  77.         final String[] dots = new String[]{
  78.                 "βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  79.                 "πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  80.                 "πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  81.                 "πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺ",
  82.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺ",
  83.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺ",
  84.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺ",
  85.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺ",
  86.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ",
  87.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ",
  88.                 "πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅"
  89.         };
  90.         final var percentageAsIntBetween0And10 = max(min((int) ceil(percentage * 10), 10), 0);
  91.         return dots[percentageAsIntBetween0And10];
  92.     }
  93.  
  94.     public void testZero() {
  95.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.00));
  96.     }
  97.  
  98.     public void testJustOverZero() {
  99.         assertEquals("πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.00001));
  100.     }
  101.  
  102.     public void test10Percent() {
  103.         assertEquals("πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.10));
  104.     }
  105.  
  106.     public void testJustOver10Percent() {
  107.         assertEquals("πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.100001));
  108.     }
  109.  
  110.     public void test20Percent() {
  111.         assertEquals("πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.20));
  112.     }
  113.  
  114.     public void testJustOver20Percent() {
  115.         assertEquals("πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.200001));
  116.     }
  117.  
  118.     public void test30Percent() {
  119.         assertEquals("πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.30));
  120.     }
  121.  
  122.     public void testJustOver30Percent() {
  123.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.300001));
  124.     }
  125.  
  126.     public void test40Percent() {
  127.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.40));
  128.     }
  129.  
  130.     public void testJustOver40Percent() {
  131.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.400001));
  132.     }
  133.  
  134.     public void test50Percent() {
  135.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(0.50));
  136.     }
  137.  
  138.     public void testJustOver50Percent() {
  139.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺ", getPercentageRounds(0.500001));
  140.     }
  141.  
  142.     public void test60Percent() {
  143.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺβšͺ", getPercentageRounds(0.60));
  144.     }
  145.  
  146.     public void testJustOver60Percent() {
  147.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺ", getPercentageRounds(0.600001));
  148.     }
  149.  
  150.     public void test70Percent() {
  151.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺβšͺ", getPercentageRounds(0.70));
  152.     }
  153.  
  154.     public void testJustOver70Percent() {
  155.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ", getPercentageRounds(0.700001));
  156.     }
  157.  
  158.     public void test80Percent() {
  159.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺβšͺ", getPercentageRounds(0.80));
  160.     }
  161.  
  162.     public void testJustOver80Percent() {
  163.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ", getPercentageRounds(0.800001));
  164.     }
  165.  
  166.     public void test90Percent() {
  167.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ", getPercentageRounds(0.90));
  168.     }
  169.  
  170.     public void testJustOver90Percent() {
  171.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(0.900001));
  172.     }
  173.  
  174.     public void testAlmost100Percent() {
  175.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(0.999999));
  176.     }
  177.  
  178.     public void test100Percent() {
  179.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(1.00));
  180.     }
  181.  
  182.     public void testJustOver100Percent() {
  183.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(1.0000001));
  184.     }
  185.  
  186.     public void test200Percent() {
  187.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(2.00));
  188.     }
  189.  
  190.     public void testCrazyLargePercentage() {
  191.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(999999.999999));
  192.     }
  193.  
  194.     public void testMaxIntegerValue() {
  195.         assertEquals("πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅πŸ”΅", getPercentageRounds(Integer.MAX_VALUE));
  196.     }
  197.  
  198.     public void testJustBelowZero() {
  199.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(-0.0000001));
  200.     }
  201.  
  202.     public void testNegative10Percent() {
  203.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(-0.10));
  204.     }
  205.  
  206.     public void testNegative20Percent() {
  207.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(-0.20));
  208.     }
  209.  
  210.     public void testCrazyLargeNegativePercentage() {
  211.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(-999999.999999));
  212.     }
  213.  
  214.     public void testMinIntegerValue() {
  215.         assertEquals("βšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺβšͺ", getPercentageRounds(Integer.MIN_VALUE));
  216.     }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement