MightyPork

testovaci trida hw03

Mar 15th, 2014
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package cz.cvut.fel.pr2.du3.testing;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.text.ParseException;
  6.  
  7. import cz.cvut.fel.pr2.du3.DataParser;
  8. import cz.cvut.fel.pr2.du3.Storage;
  9. import cz.cvut.fel.pr2.du3.objects.ArrayObject;
  10. import cz.cvut.fel.pr2.du3.objects.DataObject;
  11. import cz.cvut.fel.pr2.du3.objects.NumberObject;
  12. import cz.cvut.fel.pr2.du3.objects.StringObject;
  13.  
  14.  
  15. public class Test {
  16.  
  17.     public static void main(String[] args) throws IOException {
  18.  
  19.         testParser();
  20.         //testObjects();
  21.         //testStorage();
  22.  
  23.     }
  24.  
  25.  
  26.     private static void testStorage() {
  27.  
  28.         try {
  29.             DataProvider dp = new DataProvider("{{{lorem}}}{ipsum}{123546}");
  30.  
  31.             Storage s = new Storage(1000, dp, new DataParser());
  32.  
  33.             System.out.println(s.read(0));
  34.             System.out.println(s.read(1));
  35.             System.out.println(s.read(2));
  36.  
  37.  
  38.         } catch (IOException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.  
  44.  
  45.     private static void testParser() {
  46.  
  47.         DataParser dp = new DataParser();
  48.  
  49.         String[] tests = {
  50.                 "someTEXT012",
  51.                 "123456",
  52.                 "0",
  53.                 "{ahoj}",
  54.                 "{balik}{je}{genius}",
  55.                 "{{nested}{array}}{anotherItem}",
  56.                 "{{{{DEEP}{{{NESTING}{{LIFE}{42}}}}}}{WE}{LOVE}{LISP}}",
  57.                 "{1}{2}{3}{{{4}}}{5}{6}{7}",
  58.                 "222",
  59.                 "a22",
  60.                 "{222}{a22}",
  61.                 "{{222}{a22}}{a22}",
  62.                 // chybnÄ›
  63.                 "",
  64.                 "-3",
  65.                 "22a",
  66.                 "222 a22",
  67.                 "{222}a22",
  68.                 "{222{a22}}{a22}",
  69.                 "{{{",
  70.                 "}",
  71.                 "{{a}",
  72.                 "a}",
  73.                 "{{{15}}}}",
  74.                 ".sdg"
  75.                 "a22{222}",
  76.                 "{{5}a}{a5}a",
  77.                 "{{4}{",
  78.         };
  79.  
  80.         for (String s : tests) {
  81.             try {
  82.                 System.out.println("\nINP = " + s);
  83.  
  84.                 DataObject dobj = dp.parse(s);
  85.                 System.out.println("OUT = " + dobj.getDataString());
  86.  
  87.             } catch (ParseException e) {
  88.                 System.out.println("ParseError at " + e.getErrorOffset());
  89.             }
  90.         }
  91.        
  92.         /* EXPECTED OUTPUT
  93.          *
  94.             INP = someTEXT012
  95.             OUT = someTEXT012
  96.            
  97.             INP = 123456
  98.             OUT = 123456
  99.            
  100.             INP = 0
  101.             OUT = 0
  102.            
  103.             INP = {ahoj}
  104.             OUT = {ahoj}
  105.            
  106.             INP = {balik}{je}{genius}
  107.             OUT = {balik}{je}{genius}
  108.            
  109.             INP = {{nested}{array}}{anotherItem}
  110.             OUT = {{nested}{array}}{anotherItem}
  111.            
  112.             INP = {{{{DEEP}{{{NESTING}{{LIFE}{42}}}}}}{WE}{LOVE}{LISP}}
  113.             OUT = {{{{DEEP}{{{NESTING}{{LIFE}{42}}}}}}{WE}{LOVE}{LISP}}
  114.            
  115.             INP = {1}{2}{3}{{{4}}}{5}{6}{7}
  116.             OUT = {1}{2}{3}{{{4}}}{5}{6}{7}
  117.            
  118.             INP = 222
  119.             OUT = 222
  120.            
  121.             INP = a22
  122.             OUT = a22
  123.            
  124.             INP = {222}{a22}
  125.             OUT = {222}{a22}
  126.            
  127.             INP = {{222}{a22}}{a22}
  128.             OUT = {{222}{a22}}{a22}
  129.            
  130.             INP =
  131.             ParseError at 0
  132.            
  133.             INP = -3
  134.             ParseError at 0
  135.            
  136.             INP = 22a
  137.             ParseError at 2
  138.            
  139.             INP = 222 a22
  140.             ParseError at 3
  141.            
  142.             INP = {222}a22
  143.             ParseError at 5
  144.            
  145.             INP = {222{a22}}{a22}
  146.             ParseError at 4
  147.            
  148.             INP = {{{
  149.             ParseError at 3
  150.            
  151.             INP = }
  152.             ParseError at 0
  153.            
  154.             INP = {{a}
  155.             ParseError at 4
  156.            
  157.             INP = a}
  158.             ParseError at 1
  159.            
  160.             INP = {{{15}}}}
  161.             ParseError at 8
  162.            
  163.             INP = .sdg
  164.             ParseError at 0
  165.  
  166.             INP = a22{222}
  167.             ParseError at 3
  168.  
  169.             INP = {{5}a}{a5}a
  170.             ParseError at 4
  171.  
  172.             INP = {{4}{
  173.             ParseError at 5
  174.  
  175.          */
  176.     }
  177.  
  178.  
  179.     private static void testObjects() {
  180.  
  181.         NumberObject n;
  182.         StringObject s;
  183.  
  184.  
  185.         System.out.println("\nNumber -1");
  186.  
  187.         try {
  188.             n = new NumberObject(-1);
  189.         } catch (Throwable t) {
  190.             System.out.println(t.getMessage());
  191.         }
  192.  
  193.  
  194.         System.out.println("\nNumber 0");
  195.  
  196.         n = new NumberObject(0);
  197.  
  198.         System.out.println("getNumber: " + n.getNumber());
  199.         System.out.println("getDataString: " + n.getDataString());
  200.  
  201.  
  202.         System.out.println("\nString bad");
  203.  
  204.         try {
  205.             s = new StringObject("dfdfddf- +,fs 12");
  206.         } catch (Throwable t) {
  207.             System.out.println(t.getMessage());
  208.         }
  209.  
  210.  
  211.         System.out.println("\nString good");
  212.  
  213.         s = new StringObject("sdfhjsfdsf564g6df54gh6sdf");
  214.  
  215.         System.out.println("getString: " + s.getString());
  216.         System.out.println("getDataString: " + s.getDataString());
  217.  
  218.  
  219.         DataObject[] objs = {
  220.                 new StringObject("such"),
  221.                 new StringObject("array"),
  222.                 new ArrayObject(
  223.                         new DataObject[] {
  224.                                 new StringObject("much"),
  225.                                 new StringObject("amaze")
  226.                         }),
  227.                 new StringObject("wow"),
  228.         };
  229.  
  230.         ArrayObject ao = new ArrayObject(objs);
  231.  
  232.         System.out.println(ao.getDataString());
  233.  
  234.  
  235.     }
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment