Advertisement
JeffGrigg

Untitled

Jan 23rd, 2023
3,017
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