Advertisement
sklz0

MMN 12 Unofficial tests ver 1.2.2

Dec 4th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //To download this: press "raw" button -> save as Maman12Test.java -> place in same package with Date and FoodItem classes and run.
  2.  
  3. /**
  4.  * Fully automated tests for maman 12 (OUI).
  5.  * This class will test your Date and FoodItem classes and generate a
  6.  * report about methods working properly and failures.
  7.  *
  8.  * -------------------------------------------------------------
  9.  * NOTE: THESE TESTS ARE WRITTEN BY A STUDENT, NOT BY A TEACHER!
  10.  * RELY ON THE RESULTS AT YOUR OWN RISK!
  11.  * OFFICIAL TESTS CAN BE FOUND ON FORUM.
  12.  * --------------------------------------------------------------
  13.  *
  14.  * If you want to update these tests on pastebin.com,
  15.  * send an email to skorn.ilya AT gmail DOT com containing a copy of this file with your changes.
  16.  * Your experience and issues: https://vk.cc/a6dxBo
  17.  *
  18.  * @author Ilya Skornyakov
  19.  * @version 1.2.2
  20.  * Updates: https://pastebin.com/ND8Z7bCm
  21.  */
  22.  
  23.  
  24. public class Maman12Test {
  25.     /**
  26.      * true - display everything, false - display only failed tests and final results
  27.      */
  28.     private static final boolean VERBOSE = true;
  29.  
  30.  
  31.     private static int errorsDate = -1;
  32.     private static int errorsFoodItem = -1;
  33.     private static byte tNow = -1;
  34.  
  35.     public static void main(String[] args) {
  36.         testDate();
  37.         testFoodItem();
  38.         report();
  39.     }
  40.  
  41.  
  42.     private static void testDate() {
  43.         tNow = 0;
  44.         vPrintln(
  45.                 "\n\n=========================================\n\t\t\tTesting DATE Class\n-----------------------------------------");
  46.         errorsDate = 0;
  47.  
  48.         vPrintln("\n----= Equals =----");
  49.         Date date = new Date(1, 1, 2000);
  50.         Date date2 = new Date(1, 1, 2000);
  51.         assertTrue(date.equals(date2));
  52.         date2 = new Date(2, 1, 2000);
  53.         assertFalse(date.equals(date2));
  54.         date2 = new Date(1, 2, 2000);
  55.         assertFalse(date.equals(date2));
  56.         date2 = new Date(1, 1, 1999);
  57.         assertFalse(date.equals(date2));
  58.  
  59.         if (errorsDate > 0) {
  60.             vPrintln("FATAL ERROR: Check Date class equals method. Without this method working properly tests cant be done!");
  61.             vPrintln("Skipping Date tests");
  62.             errorsDate = -2;
  63.             return;
  64.         }
  65.  
  66.  
  67.         vPrintln("\n----= Constructor =----");
  68.         date2 = new Date(date);
  69.         assertEquals(date2, date);
  70.         assertEquals(new Date(10, 1, 999), new Date(1, 1, 2000));
  71.         assertEquals(new Date(10, 0, 2000), new Date(1, 1, 2000));
  72.         assertEquals(new Date(10, 13, 2000), new Date(1, 1, 2000));
  73.         assertEquals(new Date(0, 12, 2000), new Date(1, 1, 2000));
  74.         assertEquals(new Date(-6, 13, 2000), new Date(1, 1, 2000));
  75.         assertEquals(new Date(1, -5, 2000), new Date(1, 1, 2000));
  76.         assertEquals(new Date(32, 1, 2000), new Date(1, 1, 2000));
  77.         assertEquals(new Date(29, 2, 2001), new Date(1, 1, 2000));
  78.         assertEquals(new Date(1, 1, 10000), new Date(1, 1, 2000));
  79.         assertEquals(new Date(29, 2, 2000), new Date(29, 2, 2000));
  80.         assertEquals(new Date(10, 1, 1000), new Date(10, 1, 1000));
  81.         assertEquals(new Date(31, 12, 9999), new Date(31, 12, 9999));
  82.  
  83.         vPrintln("\n----= toString() =----");
  84.         assertEquals(new Date(15, 9, 1999).toString(), "15/09/1999");
  85.         assertEquals(new Date(1, 10, 2000).toString(), "01/10/2000");
  86.         assertEquals(new Date(20, 11, 1982).toString(), "20/11/1982");
  87.  
  88.         vPrintln("\n----= Tomorrow() =----");
  89.         assertEquals(new Date(1, 1, 2000).tomorrow(), new Date(2, 1, 2000));
  90.         assertEquals(new Date(1, 12, 2000).tomorrow(), new Date(2, 12, 2000));
  91.         assertEquals(new Date(31, 12, 2000).tomorrow(), new Date(1, 1, 2001));
  92.         assertEquals(new Date(15, 12, 2000).tomorrow(), new Date(16, 12, 2000));
  93.         assertEquals(new Date(28, 2, 2001).tomorrow(), new Date(1, 3, 2001));
  94.         assertEquals(new Date(28, 2, 2000).tomorrow(), new Date(29, 2, 2000));
  95.         assertEquals(new Date(29, 2, 2000).tomorrow(), new Date(1, 3, 2000));
  96.  
  97.         vPrintln("\n----= Difference =----");
  98.         assertEquals(new Date(1, 1, 2000).difference(new Date(1, 1, 2000)), 0);
  99.         assertEquals(new Date(1, 1, 2000).difference(new Date(2, 1, 2000)), 1);
  100.         assertEquals(new Date(31, 12, 2000).difference(new Date(1, 1, 2001)), 1);
  101.         assertEquals(new Date(28, 2, 2000).difference(new Date(29, 2, 2000)), 1);
  102.         assertEquals(new Date(28, 2, 2000).difference(new Date(1, 3, 2000)), 2);
  103.         assertEquals(new Date(28, 2, 2001).difference(new Date(1, 3, 2001)), 1);
  104.         assertEquals(new Date(1, 1, 2001).difference(new Date(1, 1, 2002)), 365);
  105.         assertEquals(new Date(1, 1, 2001).difference(new Date(1, 1, 2003)), 365 * 2);
  106.         assertEquals(new Date(1, 1, 2000).difference(new Date(1, 1, 2003)), 365 * 3 + 1);
  107.         assertEquals(new Date(1, 1, 2003).difference(new Date(1, 1, 1999)), 365 * 4 + 1);
  108.  
  109.         vPrintln("\n----= Before/After =----");
  110.         assertFalse(new Date(10, 10, 2010).before(new Date(9, 10, 2010)));
  111.         assertTrue(new Date(10, 10, 2010).before(new Date(11, 10, 2010)));
  112.         assertTrue(new Date(10, 10, 2010).after(new Date(9, 10, 2010)));
  113.         assertFalse(new Date(10, 10, 2010).after(new Date(11, 10, 2010)));
  114.         assertFalse(new Date(10, 10, 2010).after(new Date(10, 10, 2010)));
  115.         assertFalse(new Date(10, 10, 2010).before(new Date(10, 10, 2010)));
  116.         assertFalse(new Date(10, 10, 2010).before(new Date(10, 10, 2010)));
  117.         assertTrue(new Date(10, 10, 2010).after(new Date(11, 12, 2009)));
  118.         assertTrue(new Date(10, 10, 2010).after(new Date(11, 9, 2010)));
  119.         assertFalse(new Date(10, 10, 2010).after(new Date(1, 1, 2011)));
  120.         assertTrue(new Date(1, 1, 2000).after(new Date(31, 12, 1999)));
  121.         assertFalse(new Date(1, 1, 2000).before(new Date(31, 12, 1999)));
  122.  
  123.         vPrintln("\n----= Setters =----");
  124.         date = new Date(1, 1, 2001);
  125.         date.setDay(0);
  126.         assertEquals(date, new Date(1, 1, 2001));
  127.         date = new Date(1, 1, 2000);
  128.         date.setDay(31);
  129.         assertEquals(date, new Date(31, 1, 2000));
  130.         date = new Date(1, 2, 2000);
  131.         date.setDay(29);
  132.         assertEquals(date, new Date(29, 2, 2000));
  133.         date = new Date(1, 2, 2001);
  134.         date.setDay(29);
  135.         assertEquals(date, new Date(1, 2, 2001));
  136.         date = new Date(1, 1, 2001);
  137.         date.setMonth(2);
  138.         date.setMonth(0);
  139.         date.setMonth(13);
  140.         assertEquals(date, new Date(1, 2, 2001));
  141.         date.setYear(2000);
  142.         date.setYear(999);
  143.         date.setYear(10000);
  144.         assertEquals(date, new Date(1, 2, 2000));
  145.  
  146.         vPrintln("\n----=  Day of Week / Combined tests in cycle (silent) =----");
  147.         Date date3_1 = new Date(7, 1, 1989);
  148.         Date date3_2 = new Date(6, 1, 1989);
  149.         Date date3_3 = new Date(8, 1, 1989);
  150.         int dow = 0;
  151.         for (int i = 0; i < 365 * 50; i++) {
  152.             if (date3_1.dayInWeek() != dow) {
  153.                 vPrint("Error at date: %s (DOW)\t", date3_1.toString());
  154.                 assertEquals(date3_1.dayInWeek(), dow);
  155.                 break;
  156.             }
  157.  
  158.             if (!date3_2.before(date3_1)) {
  159.                 vPrint("Error at dates: %s %s (before)\t", date3_2, date3_1);
  160.                 assertTrue(date3_2.before(date3_1));
  161.                 break;
  162.             }
  163.  
  164.             if (!date3_3.after(date3_1)) {
  165.                 vPrint("Error at dates: %s %s (after)\t", date3_3, date3_1);
  166.                 assertTrue(date3_3.after(date3_1));
  167.                 break;
  168.             }
  169.  
  170.             if (date3_3.difference(date3_2) != 2) {
  171.                 vPrint("Error at dates: %s %s (difference)\t", date3_3, date3_2);
  172.                 assertEquals(date3_3.difference(date3_2), 2);
  173.                 break;
  174.             }
  175.  
  176.             date3_1 = date3_1.tomorrow();
  177.             date3_2 = date3_2.tomorrow();
  178.             date3_3 = date3_3.tomorrow();
  179.             dow = dow == 6 ? 0 : dow + 1;
  180.         }
  181.  
  182.  
  183.         tNow = -1;
  184.     }
  185.  
  186.  
  187.     private static void testFoodItem() {
  188.         tNow = 1;
  189.         vPrintln(
  190.                 "\n\n=========================================\n\t\t\tTesting FoodItem Class\n-----------------------------------------");
  191.         errorsFoodItem = 0;
  192.  
  193.         vPrintln("\n----= Equals =----");
  194.         Date date = new Date(1, 1, 2000);
  195.         Date date2 = new Date(10, 1, 2000);
  196.         Date date3 = new Date(1, 1, 2000);
  197.         Date date4 = new Date(10, 1, 2000);
  198.         FoodItem fi1 = new FoodItem("Test", 1234, 5, date, date2, 0, 20, 10);
  199.         FoodItem fi2 = new FoodItem("Test", 1234, 5, date3, date4, 0, 20, 10);
  200.         assertTrue(fi1.equals(fi2));
  201.         assertTrue(fi2.equals(fi1));
  202.         Date date5 = new Date(11, 1, 2000);
  203.         FoodItem fi3 = new FoodItem("Test", 1234, 5, date3, date5, 0, 20, 10);
  204.         assertFalse(fi1.equals(fi3));
  205.         assertFalse(fi1.equals(
  206.                 new FoodItem("Test", 1235, 5, date, date2, 0, 20, 10)));
  207.         assertFalse(fi1.equals(
  208.                 new FoodItem("Test 2", 1234, 5, date, date2, 0, 20, 10)));
  209.         assertTrue(fi1.equals(
  210.                 new FoodItem("Test", 1234, 4, date, date2, 0, 20, 10)));
  211.         assertFalse(fi1.equals(
  212.                 new FoodItem("Test", 1234, 5, date, date2, 1, 20, 10)));
  213.         assertFalse(fi1.equals(
  214.                 new FoodItem("Test", 1234, 5, date, date2, 0, 15, 10)));
  215.         assertFalse(fi1.equals(
  216.                 new FoodItem("Test", 1234, 5, date, date2, 0, 20, 20)));
  217.         if (errorsFoodItem > 0) {
  218.             vPrintln("FATAL ERROR: Check FoodItem class equals method. Without this method working properly tests can't be done!");
  219.             vPrintln("Skipping FoodItem tests");
  220.             errorsFoodItem = -2;
  221.             return;
  222.         }
  223.  
  224.         vPrintln("\n----= Constructor =----");
  225.         FoodItem fi4 = new FoodItem(fi2);
  226.         assertEquals(fi4, fi2);
  227.         assertEquals(new FoodItem("Test", 999, -1, date, date2, 0, 20, -5),
  228.                 new FoodItem("Test", 9999, 0, date, date2, 0, 20, 1));
  229.         assertEquals(new FoodItem("", 10000, 0, date, date2, 10, 0, 0),
  230.                 new FoodItem("item", 9999, 0, date, date2, 0, 10, 1));
  231.         Date date6 = new Date(10, 1, 2000);
  232.         Date date7 = new Date(9, 1, 2000);
  233.         Date date8 = new Date(11, 1, 2000);
  234.         assertEquals(new FoodItem("Test", 1234, 5, date6, date7, 0, 20, 10),
  235.                 new FoodItem("Test", 1234, 5, date6, date8, 0, 20, 10));
  236.  
  237.         vPrintln("\n----= Setters =----");
  238.         fi4 = new FoodItem("Test", 1234, 5, date, date2, 0, 20, 10);
  239.         fi4.setPrice(0);
  240.         assertEquals(fi4.getPrice(), 10);
  241.         fi4.setPrice(-5);
  242.         assertEquals(fi4.getPrice(), 10);
  243.         fi4.setPrice(5);
  244.         assertEquals(fi4.getPrice(), 5);
  245.         fi4.setQuantity(-1);
  246.         assertEquals(fi4.getQuantity(), 5);
  247.         fi4.setQuantity(6);
  248.         assertEquals(fi4.getQuantity(), 6);
  249.         fi4.setProductionDate(date8);
  250.         assertEquals(fi4.getProductionDate(), date);
  251.         fi4.setExpiryDate(new Date(30, 12, 1999));
  252.         assertEquals(fi4.getExpiryDate(), date2);
  253.  
  254.         vPrintln("\n----= toString() =----");
  255.         FoodItem fi5 = new FoodItem("milk", 1234, 3, new Date(14, 12, 2019), new Date(21, 12, 2019), 10, 15, 5);
  256.         assertEquals(fi5.toString(), "FoodItem: milk\tCatalogueNumber: 1234\tProductionDate: 14/12/2019\tExpiryDate: 21/12/2019\tQuantity: 3");
  257.  
  258.         vPrintln("\n----= isFresh() =----");
  259.         assertTrue(fi5.isFresh(new Date(20, 12, 2019)));
  260.         assertTrue(fi5.isFresh(new Date(21, 12, 2019)));
  261.         assertTrue(fi5.isFresh(new Date(14, 12, 2019)));
  262.         assertFalse(fi5.isFresh(new Date(22, 12, 2019)));
  263.         assertFalse(fi5.isFresh(new Date(13, 12, 2019)));
  264.  
  265.         vPrintln("\n----= isCheaper() =----");
  266.         FoodItem fi6 = new FoodItem("Test", 1234, 5, date, date2, 0, 20, 5);
  267.         assertFalse(fi6.isCheaper(fi5));
  268.         assertFalse(fi5.isCheaper(fi6));
  269.         assertFalse(fi4.isCheaper(fi5));
  270.         assertTrue(fi6.isCheaper(fi2));
  271.  
  272.         vPrintln("\n----= howManyItems() =----");
  273.         assertEquals(fi6.howManyItems(0), 0);
  274.         assertEquals(fi6.howManyItems(-10), 0);
  275.         assertEquals(fi6.howManyItems(4), 0);
  276.         assertEquals(fi6.howManyItems(5), 1);
  277.         assertEquals(fi6.howManyItems(9), 1);
  278.         assertEquals(fi6.howManyItems(10), 2);
  279.         assertEquals(fi6.howManyItems(24), 4);
  280.         assertEquals(fi6.howManyItems(25), 5);
  281.         assertEquals(fi6.howManyItems(100), 5);
  282.  
  283.         vPrintln("\n----= olderFoodItem() =----");
  284.         assertTrue(fi6.olderFoodItem(fi5));
  285.         assertFalse(fi5.olderFoodItem(fi6));
  286.         assertFalse(fi5.olderFoodItem(fi5));
  287.  
  288.  
  289.         tNow = -1;
  290.     }
  291.  
  292.     private static void assertEquals(Date d1, Date d2) {
  293.         if (d1.equals(d2))
  294.             vPrint(false, "%s and %s are equal\t-> ok\n", d1, d2);
  295.         else {
  296.             vPrint("***  %s and %s are NOT equal\t-> ERROR  ***\n", d1, d2);
  297.             addError();
  298.         }
  299.  
  300.     }
  301.  
  302.     private static void assertEquals(FoodItem d1, FoodItem d2) {
  303.         if (d1.equals(d2))
  304.             vPrint(false, "%s and %s are equal\t-> ok\n", d1, d2);
  305.         else {
  306.             vPrint("***  %s and %s are NOT equal\t-> ERROR  ***\n", d1, d2);
  307.             addError();
  308.  
  309.         }
  310.  
  311.     }
  312.  
  313.     private static void assertEquals(String a, String b) {
  314.         if (a.equals(b))
  315.             vPrint(false, "%s and %s are equal\t-> ok\n", a, b);
  316.         else {
  317.             vPrint("***  %s and %s are NOT equal\t-> ERROR  ***\n", a, b);
  318.             addError();
  319.  
  320.         }
  321.  
  322.     }
  323.  
  324.     private static void assertEquals(int a, int b) {
  325.         if (a == b)
  326.             vPrint(false, "%s and %s are equal\t-> ok\n", a, b);
  327.         else {
  328.             vPrint("***  %s and %s are NOT equal\t-> ERROR  ***\n", a, b);
  329.             addError();
  330.         }
  331.  
  332.     }
  333.  
  334.     private static void assertTrue(boolean s1) {
  335.         if (s1)
  336.             vPrint(false, "True\t-> ok\n");
  337.         else {
  338.             vPrint("***  FALSE (expected true)\t-> ERROR  ***\n");
  339.             addError();
  340.  
  341.         }
  342.     }
  343.  
  344.     private static void assertFalse(boolean s1) {
  345.         if (!s1)
  346.             vPrint(false, "False\t-> ok\n");
  347.         else {
  348.             vPrint("***  TRUE (expected false)\t-> ERROR  ***\n");
  349.             addError();
  350.         }
  351.     }
  352.  
  353.     private static void addError() {
  354.         if (tNow == 0) errorsDate++;
  355.         if (tNow == 1) errorsFoodItem++;
  356.     }
  357.  
  358.     private static void vPrintln(String s) {
  359.         vPrintln(true, s);
  360.     }
  361.  
  362.     private static void vPrintln(Boolean v, String s) {
  363.         vPrint(v, s + "\n");
  364.     }
  365.  
  366.     private static void vPrint(String s, Object... o) {
  367.         vPrint(true, s, o);
  368.     }
  369.  
  370.     private static void vPrint(Boolean v, String s, Object... o) {
  371.         if (v || VERBOSE)
  372.             System.out.printf(s, o);
  373.     }
  374.  
  375.     private static void report() {
  376.         vPrintln("\n\n=========================================\n\t\t\tTESTS RESULTS\n-----------------------------------------");
  377.         vPrintln("--> DATE CLASS");
  378.         report(errorsDate);
  379.         vPrintln("\n--> FOODITEM CLASS");
  380.         report(errorsFoodItem);
  381.         vPrintln("-----------------------------------------");
  382.         if (errorsDate < 0 || errorsFoodItem < 0)
  383.             vPrint("\tSome classes weren't tested\n");
  384.         else {
  385.             int e = errorsDate + errorsFoodItem;
  386.             if (e > 0)
  387.                 vPrint("\t\t\tTotal errors: %d\n", e);
  388.             else
  389.                 vPrint("All classes passed tests with no errors\n");
  390.         }
  391.  
  392.         vPrintln("=========================================");
  393.         vPrintln("What was your experience? Let me know: https://vk.cc/a6dxBo");
  394.         vPrintln("\n~~ Please check your classes with official tester on forum ~~");
  395.     }
  396.  
  397.     private static void report(int errors) {
  398.         if (errors == -2) {
  399.             vPrintln("Testing failed due to bad equals() method");
  400.  
  401.         } else if (errors == -1) {
  402.             vPrintln("Tests weren't performed");
  403.         } else {
  404.             if (errors > 0)
  405.                 vPrint("✖ Tests revealed %d errors ᕙ(⇀‸↼‶)ᕗ\n", errors);
  406.             else
  407.                 vPrintln("✓ Tests are passed with no errors ┐( ̄ヮ ̄)┌");
  408.         }
  409.     }
  410. }
  411.  
  412. /* CHANGELOG
  413. * 1.2.2 - FoodItem equals test fixed (quantity)
  414. * 1.2.1 - difference test fixed
  415. * */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement