SHARE
TWEET

Test suite for grammar generator

dechicom Feb 13th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2. import unittest
  3. import sys
  4. import os
  5. import traceback
  6. import coverage
  7. import pprint
  8.  
  9. # import specific package modules
  10. #sys.path.insert(0,"../")
  11. from Egramgen import Gramgen
  12.  
  13. class TestSequenceClient(unittest.TestCase ):
  14.  
  15.     def setUp(self):
  16.         self.pp = pprint.PrettyPrinter(indent=4)
  17.         pass
  18.     def tearDown(self):
  19.         pass
  20.  
  21.     def testA1Gramgen(self):
  22.         maxlen=4
  23.         S="S"
  24.         Vn=["S","A","B"] # non terminal
  25.         Vt=["a","b"]     # terminal      
  26.         R=[("S","aB"),  ("A","a"),("A","aS"),("A","bAA"),
  27.            ("S","bA"),("B","b"),("B","bS"),("B","aBB")]
  28.         #Na(P) =Nb(P)
  29.         Lg=Gramgen(Vn,Vt,R,S,maxlen)
  30.         Lg.gen()
  31.         Lg.L
  32.         L=Lg.L
  33.         L.sort()
  34.         # creating what should be obtained
  35.         # langage with n=word-length <=maxlen
  36.         LL=list()
  37.         import itertools as IT
  38.         for n in range(2,maxlen+2,2):
  39.             positions=range(n)
  40.             combinations=list(IT.combinations(positions,n/2))
  41.             for a_positions in combinations:
  42.                 word=["a" if i in a_positions else "b" for i in positions]
  43.                 LL.append(word)
  44.  
  45.         LL=[''.join(s) for s in LL]
  46.         LL.sort()
  47.         self.assertEqual(L,LL,"result Na(P)=Nb(P) not ok")
  48.        
  49.     def testA2Gramgen(self):
  50.         #L={"a"*n"b"*n"c"*n|n is integer}
  51.         maxn=3
  52.         maxlen=3*maxn
  53.         S="S"
  54.         Vn=["S","X","Y"] # non terminal
  55.         Vt=["a","b","c"]     # terminal      
  56.         R=[("S","abc"),  ("S","aXbc"),("Xb","bX"),("Xc","Ybcc"),
  57.            ("bY","Yb"),("aY","aaX"),("aY","aa")]
  58.         Lg=Gramgen(Vn,Vt,R,S,maxlen)
  59.         Lg.gen()
  60.         Lg.L
  61.         L=Lg.L
  62.         L.sort()
  63.         # creating what should be obtained
  64.         # langage with n=word-length <=maxlen
  65.         LL=list()
  66.         import itertools as IT
  67.         for n in range(1,maxn+1):
  68.             word="a"*n+"b"*n+"c"*n
  69.             LL.append(word)
  70.  
  71.         LL=[''.join(s) for s in LL]
  72.         LL.sort()
  73.         #print L
  74.         #print LL
  75.         self.assertEqual(L,LL,'result "a"*n"b"*n"c"*n not ok')
  76.     def testA3Gramgen(self):
  77.         '''
  78.        L={ a^n n integer   }
  79.        '''
  80.         maxlen=20
  81.         S="S"
  82.         Vn=["S","A"] # non terminal
  83.         Vt=["a",]     # terminal
  84.         print Vn,Vt
  85.         L=list()      
  86.         R=[("S","a"),
  87.            ("S","Aa"),
  88.            ("A","a"),
  89.            ("A","Aa")
  90.         ]
  91.         Lg=Gramgen(Vn,Vt,R,S,maxlen)
  92.         Lg.gen()
  93.         Lg.L.sort()
  94.         LL=list()
  95.         for i in xrange(1,21):
  96.             LL.append("a"*i)
  97.         LL.sort()
  98.         print "Lg.l\n",        Lg.L
  99.         print "LL \n",LL
  100.         self.assertEqual(Lg.L,LL,"result 'a'*n not ok")
  101.  
  102.    
  103. '''        
  104.    def testA3arangements_ie(self):
  105.        a=CT.arangements_ie(10,4,3)
  106.        self.assertEqual(a,25200,"resultnot ok: com (%i,%i,%i)<>%i" %(10,4,3,a))
  107.        self.assertRaises(CT.CountingValueError,CT.arangements_ie,10,4,7)
  108. '''        
  109.    
  110.  
  111. try:
  112.  
  113.     suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceClient)
  114.  
  115.     global cov
  116.     cov=coverage.coverage(cover_pylib=False,timid=True,source=['Egramgen'])
  117.     cov.start()
  118.     unittest.TextTestRunner(verbosity=2).run(suite)
  119.     cov.stop()
  120.     cov.html_report(directory="./coverage")
  121.     #cov.report()
  122. except:
  123.     pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top