Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.57 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class FrequencyBagTester
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         int numberOfData = 20;
  9.         int maxValueEx = 5;
  10.         boolean wrongFrequency = false;
  11.         int failed = 0;
  12.         Scanner in = new Scanner(System.in);
  13.         int option = 0;
  14.        
  15.         while(option == 0)
  16.         {
  17.             System.out.println("Please select a test option");
  18.             System.out.println("  1. Stop the test as soon as an error is detected");
  19.             System.out.println("  2. Do not stop the test when an error is detected");
  20.             System.out.print("Enter an option (1 or 2): ");
  21.             option = in.nextInt();
  22.            
  23.             if(option != 1 && option != 2)
  24.             {
  25.                 System.out.println(option + " is not a valid choice.\n");
  26.                 option = 0;
  27.             }
  28.         }
  29.        
  30.         in.close();
  31.        
  32.         FrequencyBag<Integer> fb = new FrequencyBag<Integer>();
  33.        
  34.         // getFrequencyOf() empty bag
  35.        
  36.         System.out.print("Checking the method getFrequencyOf() of an empty frequency bag: ");
  37.         for(int i = 0; i < maxValueEx; i++)
  38.         {
  39.             if(fb.getFrequencyOf(i) != 0)
  40.             {
  41.                 if(!wrongFrequency)
  42.                 {
  43.                     System.out.println("FAIL");
  44.                     failed++;
  45.                 }
  46.                 System.out.println("The method getFrequencyOf(" + i + ") of an empty bag should return 0.");
  47.                 System.out.println("But your method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".");
  48.                 wrongFrequency = true;
  49.             }
  50.         }
  51.        
  52.         if(!wrongFrequency)
  53.         {
  54.             System.out.println("PASS");
  55.         }
  56.         else if(option == 1)
  57.         {
  58.             return;
  59.         }
  60.        
  61.        
  62.         // size() of empty bag.
  63.        
  64.         System.out.print("Checking the method size() of an empty bag: ");
  65.         if(fb.size() != 0)
  66.         {
  67.             failed++;
  68.             System.out.println("FAIL");
  69.             System.out.println("The size of an empty bag should be 0.");
  70.             System.out.println("But your method size() returns " + fb.size() + ".\n");
  71.             if(option == 1)
  72.             {
  73.                 return;
  74.             }
  75.         }
  76.         else
  77.         {
  78.             System.out.println("PASS");
  79.         }
  80.        
  81.         // getMaxFrequency() of empty bag
  82.        
  83.         System.out.print("Checking the method getMaxFrequency() of an empty frequency bag: ");
  84.         if(fb.getMaxFrequency() != 0)
  85.         {
  86.             failed++;
  87.             System.out.println("FAIL");
  88.             System.out.println("The method getMaxFrequency() of an empty bag should return 0.");
  89.             System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
  90.             if(option == 1)
  91.             {
  92.                 return;
  93.             }
  94.         }
  95.         else
  96.         {
  97.             System.out.println("PASS\n");
  98.         }
  99.        
  100.         // Adding data into empty bag
  101.        
  102.         int dataSet[] = new int[5];
  103.        
  104.         for(int i = 0; i < maxValueEx; i++)
  105.         {
  106.             dataSet[i] = 0;
  107.         }
  108.        
  109.         Random rand = new Random();
  110.        
  111.         System.out.println("Add the following data into your frequency bag (in that order):");
  112.        
  113.         for(int i = 0; i < numberOfData; i++)
  114.         {
  115.             int temp = rand.nextInt(maxValueEx);
  116.            
  117.             System.out.print(temp + " ");
  118.             fb.add(temp);
  119.             dataSet[temp]++;
  120.         }
  121.         System.out.println();
  122.        
  123.         // getFrequencyOf()
  124.        
  125.         System.out.println("Checking the method getFrequencyOf() of each data in this frequency bag: ");
  126.        
  127.         for(int i = 0; i < maxValueEx; i++)
  128.         {
  129.             System.out.print("Frequency of " + i + ": ");
  130.             if(fb.getFrequencyOf(i) != dataSet[i])
  131.             {
  132.                 failed++;
  133.                 System.out.println("FAIL");
  134.                 System.out.println("The frequency of " + i + " is " + dataSet[i] + ".");
  135.                 System.out.println("But the method getFrequencyOf(" + i + ") returns " + fb.getFrequencyOf(i) + ".\n");
  136.                 if(option == 1)
  137.                 {
  138.                     return;
  139.                 }
  140.             }
  141.             else
  142.             {
  143.                 System.out.println("PASS");
  144.             }
  145.         }
  146.  
  147.         // getFrequencyOf()
  148.        
  149.         System.out.print("Checking the method getFrequencyOf() of a data that is not in this frequency bag: ");
  150.        
  151.         if(fb.getFrequencyOf(-1) != 0)
  152.         {
  153.             failed++;
  154.             System.out.println("FAIL");
  155.             System.out.println("The frequency of -1 is 0.");
  156.             System.out.println("But the method getFrequencyOf(-1) returns " + fb.getFrequencyOf(-1) + ".\n");
  157.             if(option == 1)
  158.             {
  159.                 return;
  160.             }
  161.         }
  162.         else
  163.         {
  164.             System.out.println("PASS");
  165.         }
  166.  
  167.         // getProbabilityOf()
  168.        
  169.         System.out.println("Checking the method getProbabilityOf() of each data in this frequency bag: ");
  170.        
  171.         for(int i = 0; i < maxValueEx; i++)
  172.         {
  173.             System.out.print("Probability of " + i + ": ");
  174.             if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
  175.             {
  176.                 failed++;
  177.                 System.out.println("FAIL");
  178.                 System.out.println("The probability of " + i + " is " + ((double) dataSet[i]/numberOfData) + ".");
  179.                 System.out.println("But the method getProbabilityOf(" + i + ") returns " + fb.getProbabilityOf(i) + ".\n");
  180.                 if(option == 1)
  181.                 {
  182.                     return;
  183.                 }
  184.             }
  185.             else
  186.             {
  187.                 System.out.println("PASS");
  188.             }
  189.         }
  190.        
  191.         // getProbabilityOf()
  192.        
  193.         System.out.print("Checking the method getProbabilityOf() of a data that is not in this frequency bag: ");
  194.        
  195.         if(fb.getProbabilityOf(-1) != 0.0)
  196.         {
  197.             failed++;
  198.             System.out.println("FAIL");
  199.             System.out.println("The probability of -1 is 0.");
  200.             System.out.println("But the method getProbabilityOf(-1) returns " + fb.getProbabilityOf(-1) + ".\n");
  201.             if(option == 1)
  202.             {
  203.                 return;
  204.             }
  205.         }
  206.         else
  207.         {
  208.             System.out.println("PASS");
  209.         }
  210.        
  211.         // size()
  212.  
  213.         System.out.print("Checking the method size() of this non-empty frequency bag: ");
  214.         if(fb.size() != numberOfData)
  215.         {
  216.             failed++;
  217.             System.out.println("FAIL");
  218.             System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
  219.             System.out.println("But your method size() returns " + fb.size() + ".\n");
  220.             if(option == 1)
  221.             {
  222.                 return;
  223.             }
  224.         }
  225.         else
  226.         {
  227.             System.out.println("PASS");
  228.         }
  229.        
  230.         // getMaxFrequency()
  231.        
  232.         System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
  233.        
  234.         int tempMax = 0;
  235.        
  236.         for(int i = 0; i < maxValueEx; i++)
  237.         {
  238.             if(tempMax < dataSet[i])
  239.             {
  240.                 tempMax = dataSet[i];
  241.             }
  242.         }
  243.        
  244.         if(fb.getMaxFrequency() != tempMax)
  245.         {
  246.             failed++;
  247.             System.out.println("FAIL");
  248.             System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
  249.             System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
  250.             if(option == 1)
  251.             {
  252.                 return;
  253.             }
  254.         }
  255.         else
  256.         {
  257.             System.out.println("PASS\n");
  258.         }
  259.        
  260.         // clear();
  261.        
  262.         fb.clear();
  263.        
  264.         System.out.println("Checking the method clear()");
  265.         System.out.print("Checking the method size() after clear(): ");
  266.         if(fb.size() != 0)
  267.         {
  268.             failed++;
  269.             System.out.println("FAIL");
  270.             System.out.println("After the bag is cleared, the method size() should return 0.");
  271.             System.out.println("But your method size() returns " + fb.size() + ".\n");
  272.             if(option == 1)
  273.             {
  274.                 return;
  275.             }
  276.         }
  277.         else
  278.         {
  279.             System.out.println("PASS");
  280.         }
  281.        
  282.         // getFrequencyOf() after clear()
  283.        
  284.         System.out.print("Checking the method getFrequencyOf(3) after clear(): ");
  285.         if(fb.getFrequencyOf(3) != 0)
  286.         {
  287.             failed++;
  288.             System.out.println("FAIL");
  289.             System.out.println("After the bag is cleared, the method getFrequencyOf(3) should return 0.");
  290.             System.out.println("But your method getFrequencyOf(3) returns " + fb.getFrequencyOf(3) + ".\n");
  291.             if(option == 1)
  292.             {
  293.                 return;
  294.             }
  295.         }
  296.         else
  297.         {
  298.             System.out.println("PASS");
  299.         }
  300.        
  301.         // getMaxFrequency() after clear()
  302.        
  303.         System.out.print("Checking the method getMaxFrequency() after clear(): ");
  304.         if(fb.getMaxFrequency() != 0)
  305.         {
  306.             failed++;
  307.             System.out.println("FAIL");
  308.             System.out.println("After the bag is cleared, the method getMaxFrequency() should return 0.");
  309.             System.out.println("But your method getMaxFrequency() returns " + fb.getMaxFrequency() + ".\n");
  310.             if(option == 1)
  311.             {
  312.                 return;
  313.             }
  314.         }
  315.         else
  316.         {
  317.             System.out.println("PASS");
  318.         }
  319.        
  320.         // Generate large bag
  321.        
  322.         numberOfData = 1000000;
  323.         maxValueEx = 1000;
  324.        
  325.         dataSet = new int[maxValueEx];
  326.        
  327.         for(int i = 0; i < maxValueEx; i++)
  328.         {
  329.             dataSet[i] = 0;
  330.         }
  331.        
  332.         System.out.println("\nSo far so good.");
  333.         System.out.println("Let's try adding " + numberOfData + " data into your frequency bag.");
  334.        
  335.         for(int i = 0; i < numberOfData; i++)
  336.         {
  337.             int temp = rand.nextInt(maxValueEx);
  338.            
  339.             fb.add(temp);
  340.             dataSet[temp]++;
  341.         }
  342.  
  343.         // size() of large bag.
  344.        
  345.         System.out.print("Checking the method size() of this non-empty frequency bag: ");
  346.         if(fb.size() != numberOfData)
  347.         {
  348.             failed++;
  349.             System.out.println("FAIL");
  350.             System.out.println("After adding " + numberOfData + " entries, the method size() should return " + numberOfData + ".");
  351.             System.out.println("But your method size() returns " + fb.size() + ".\n");
  352.             if(option == 1)
  353.             {
  354.                 return;
  355.             }
  356.         }
  357.         else
  358.         {
  359.             System.out.println("PASS");
  360.         }
  361.  
  362.         // getFrequencyOf() of large bag
  363.        
  364.         System.out.print("Checking the method getFrequencyOf() of each data in this frequency bag: ");
  365.  
  366.         wrongFrequency = false;
  367.        
  368.         for(int i = 0; i < maxValueEx; i++)
  369.         {
  370.             if(fb.getFrequencyOf(i) != dataSet[i])
  371.             {
  372.                 wrongFrequency = true;
  373.             }
  374.         }
  375.  
  376.         if(wrongFrequency)
  377.         {
  378.             failed++;
  379.             System.out.println("FAIL");
  380.             if(option == 1)
  381.             {
  382.                 return;
  383.             }
  384.         }
  385.         else
  386.         {
  387.             System.out.println("PASS");
  388.         }
  389.        
  390.         // getProbabilityOf() of large bag
  391.        
  392.         System.out.print("Checking the method getProbabilityOf() of each data in this frequency bag: ");
  393.  
  394.         boolean wrongProbability = false;
  395.        
  396.         for(int i = 0; i < maxValueEx; i++)
  397.         {
  398.             if(Math.abs(fb.getProbabilityOf(i) - ((double) dataSet[i]/numberOfData)) > 0.000001)
  399.             {
  400.                 wrongProbability = true;
  401.             }
  402.         }
  403.  
  404.         if(wrongProbability)
  405.         {
  406.             failed++;
  407.             System.out.println("FAIL");
  408.             if(option == 1)
  409.             {
  410.                 return;
  411.             }
  412.         }
  413.         else
  414.         {
  415.             System.out.println("PASS");
  416.         }
  417.        
  418.         // getMaxFrequency() of large bag
  419.        
  420.         System.out.print("Checking the method getMaxFrequency() of this non-empty frequency bag: ");
  421.        
  422.         tempMax = 0;
  423.        
  424.         for(int i = 0; i < maxValueEx; i++)
  425.         {
  426.             if(tempMax < dataSet[i])
  427.             {
  428.                 tempMax = dataSet[i];
  429.             }
  430.         }
  431.        
  432.         if(fb.getMaxFrequency() != tempMax)
  433.         {
  434.             failed++;
  435.             System.out.println("FAIL");
  436.             System.out.println("The method getMaxFrequency() of this non-empty frequency bag should return " + tempMax + ".");
  437.             System.out.println("But your method getMaxFrequency() of this non-empty frequency bag returns " + fb.getMaxFrequency() + ".\n");
  438.             if(option == 1)
  439.             {
  440.                 return;
  441.             }
  442.         }
  443.         else
  444.         {
  445.             System.out.println("PASS");
  446.         }
  447.  
  448.         // Frequency Bag of String
  449.        
  450.         System.out.println("\nSo far so good.");
  451.         System.out.println("Let's construct a frequency bag of String.");
  452.        
  453.         FrequencyBag<String> sfb = new FrequencyBag<String>();
  454.        
  455.         String s = "hello how are you i am find thank you and you i am fine thank you";
  456.         String[] str = s.split(" ");
  457.        
  458.         for(int i = 0; i < str.length; i++)
  459.         {
  460.             sfb.add(str[i]);
  461.         }
  462.        
  463.         System.out.println("Adding the following strings into an empty frequency bag:");
  464.         System.out.println(s + "\n");
  465.        
  466.         System.out.print("Checking the method size() of this non-empty frequency bag: ");
  467.         if(sfb.size() != 16)
  468.         {
  469.             failed++;
  470.             System.out.println("FAIL");
  471.             System.out.println("After adding 16 entries, the method size() should return 16.");
  472.             System.out.println("But your method size() returns " + sfb.size() + ".\n");
  473.             if(option == 1)
  474.             {
  475.                 return;
  476.             }
  477.         }
  478.         else
  479.         {
  480.             System.out.println("PASS");
  481.         }
  482.        
  483.         System.out.print("Checking the method getFrequencyOf(\"am\"): ");
  484.         if(sfb.getFrequencyOf("am") != 2)
  485.         {
  486.             failed++;
  487.             System.out.println("FAIL");
  488.             System.out.println("There are two strings \"am\" added into this bag.");
  489.             System.out.println("But your method getFrequencyOf(\"am\") returns " + sfb.getFrequencyOf("am") + ".\n");
  490.             if(option == 1)
  491.             {
  492.                 return;
  493.             }
  494.         }
  495.         else
  496.         {
  497.             System.out.println("PASS");
  498.         }
  499.        
  500.         System.out.print("Checking the method getMaxFrequency(): ");
  501.         if(sfb.getMaxFrequency() != 4)
  502.         {
  503.             failed++;
  504.             System.out.println("FAIL");
  505.             System.out.println("The maximum frequency should be 4 (the string \"you\").");
  506.             System.out.println("But your method getMaxFrequency() returns " + sfb.getMaxFrequency() + ".\n");
  507.             if(option == 1)
  508.             {
  509.                 return;
  510.             }
  511.         }
  512.         else
  513.         {
  514.             System.out.println("PASS");
  515.         }
  516.        
  517.         System.out.println();
  518.        
  519.         if(failed != 0)
  520.         {
  521.             System.out.println("Threre are " + failed + " in this test. Fix your program.");
  522.         }
  523.         else
  524.         {
  525.             System.out.println("Congratulation!!! Your FrequencyBag works perfectly (I hope).");
  526.             System.out.println("Run the program FrequencyFrame and compare its result.");
  527.         }
  528.     }
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement