/[gzz]/gzz/test/gzz/media/enfilade.py
ViewVC logotype

Diff of /gzz/test/gzz/media/enfilade.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by benja, Sun Aug 18 12:00:30 2002 UTC revision 1.10 by tjl, Sun Oct 13 12:39:27 2002 UTC
# Line 28  import unittest Line 28  import unittest
28  from java.util import Random, ArrayList  from java.util import Random, ArrayList
29  from java import io  from java import io
30    
31  class TestEnfilade(unittest.TestCase):  # Requires attributes:
32      # Requires attributes:  # spanMaker: a SpanMaker
33      # spanMaker: a SpanMaker  # maker: Enfilade1D.Maker
34      # maker: Enfilade1D.Maker  
35      def make(self, span):  def make( span):
36          if isinstance(span, type("")):      if isinstance(span, type("")):
37              span = self.makeSpan(span)          span = makeSpan(span)
38          return self.maker.makeEnfilade(span)      return maker.makeEnfilade(span)
39      def makeSpan(self, text):  
40          return self.spanMaker.makeTextSpan(text)  def makeSpan(text):
41        return spanMaker.makeTextSpan(text)
42      def bigEnf(self):  
43          """Construct a large set of random enfilades.  def bigEnf():
44        """Construct a large set of random enfilades.
45          We have to be careful to include all types of nodes there."""  
46        We have to be careful to include all types of nodes there."""
47          rand = Random(42)  
48          s = ["foo", "bar", "x", ""]      rand = Random(42)
49          sp = ["graf", "barg", "i", ""]      s = ["foo", "bar", "x", ""]
50          e = [self.make(x) for x in s]      sp = ["graf", "barg", "i", ""]
51          ep = [self.makeSpan(x) for x in sp]      e = [make(x) for x in s]
52          for i in range(0,80):      ep = [makeSpan(x) for x in sp]
53              r1 = rand.nextInt(len(s))      for i in range(0,80):
54            r1 = rand.nextInt(len(s))
55            if rand.nextBoolean():
56                # append two enfilades
57                r2 = rand.nextInt(len(s))
58                s.append(s[r1] + s[r2])
59                e.append(e[r1].plus(e[r2]))
60            else:
61                # Append or prepend a span
62                r2 = rand.nextInt(len(sp))
63              if rand.nextBoolean():              if rand.nextBoolean():
64                  # append two enfilades                  s.append(s[r1] + sp[r2])
65                  r2 = rand.nextInt(len(s))                  e.append(e[r1].plus(ep[r2]))
                 s.append(s[r1] + s[r2])  
                 e.append(e[r1].plus(e[r2]))  
66              else:              else:
67                  # Append or prepend a span                  s.append(sp[r2] + s[r1])
68                  r2 = rand.nextInt(len(sp))                  e.append(e[r1].prepended(ep[r2]))
69                  if rand.nextBoolean():      return (s, sp, e, ep)
70                      s.append(s[r1] + sp[r2])  
71                      e.append(e[r1].plus(ep[r2]))  def testTrivial():
72                  else:      "Test enfilades consisting of no spans and a single span."
73                      s.append(sp[r2] + s[r1])      nullEnf = make(None)
74                      e.append(e[r1].prepended(ep[r2]))      failUnlessEqual(0, nullEnf.length())
75          return (s, sp, e, ep)      failUnlessEqual("", nullEnf.makeString())
76        failIf(nullEnf.getList().iterator().hasNext())
77      def testTrivial(self):  
78          "Test enfilades consisting of no spans and a single span."      sEnf = make("foo")
79          nullEnf = self.make(None)      failUnlessEqual("foo", sEnf.makeString())
80          self.failUnlessEqual(0, nullEnf.length())      failUnlessEqual(3, sEnf.length())
81          self.failUnlessEqual("", nullEnf.makeString())  
82          self.failIf(nullEnf.getList().iterator().hasNext())  def testPlus():
83        "Test concatenation"
84          sEnf = self.make("foo")      e1 = make("foo")
85          self.failUnlessEqual("foo", sEnf.makeString())      e2 = make("bar")
86          self.failUnlessEqual(3, sEnf.length())      e3 = make("baz")
87    
88      def testPlus(self):      s1 = makeSpan("frob")
89          "Test concatenation"  
90          e1 = self.make("foo")      failUnlessEqual("foobar", e1.plus(e2).makeString())
91          e2 = self.make("bar")      failUnlessEqual("foobarbaz", e1.plus(e2).plus(e3).makeString())
92          e3 = self.make("baz")      failUnlessEqual("foobarbaz", e1.plus(e2.plus(e3)).makeString())
93        failUnlessEqual("foofrobbar", e1.plus(s1).plus(e2).makeString())
94          s1 = self.makeSpan("frob")  
95        failUnlessEqual(e1.plus(e2).plus(e3), e1.plus(e2.plus(e3)))
96          self.failUnlessEqual("foobar", e1.plus(e2).makeString())      # cannot do the next test: the transientscrolls don't have hashs yet
97          self.failUnlessEqual("foobarbaz", e1.plus(e2).plus(e3).makeString())      #failUnlessEqual(e1.plus(e2).plus(e3).hashCode(), e1.plus(e2.plus(e3)).hashCode())
98          self.failUnlessEqual("foobarbaz", e1.plus(e2.plus(e3)).makeString())  
99          self.failUnlessEqual("foofrobbar", e1.plus(s1).plus(e2).makeString())  def testSub():
100        "Test subenfilades"
101          self.failUnlessEqual(e1.plus(e2).plus(e3), e1.plus(e2.plus(e3)))      # see that they and all their substrings are equal.
102          # cannot do the next test: the transientscrolls don't have hashs yet      s, sp, e, ep = bigEnf()
103          #self.failUnlessEqual(e1.plus(e2).plus(e3).hashCode(), e1.plus(e2.plus(e3)).hashCode())      for i in range(0,len(s)):
104            failUnlessEqual(s[i], e[i].makeString())
105      def testSub(self):          for enf in (e[i], e[i].optimized()):
106          "Test subenfilades"              for x in range(0,len(s[i])):
107          # see that they and all their substrings are equal.                  failUnlessEqual(s[i][x:], enf.sub(x).makeString())
108          s, sp, e, ep = self.bigEnf()                  for y in range(x, len(s[i])):
109          for i in range(0,len(s)):                      failUnlessEqual(s[i][x:y],
110              self.failUnlessEqual(s[i], e[i].makeString())                                      enf.sub(x,y).makeString())
111              for enf in (e[i], e[i].optimized()):  
112                  for x in range(0,len(s[i])):  def testFromList():
113                      self.failUnlessEqual(s[i][x:], enf.sub(x).makeString())      x = [makeSpan(e) for e in ("ng ", "fe", "i", "shu")]
114                      for y in range(x, len(s[i])):      l = ArrayList()
115                          self.failUnlessEqual(s[i][x:y],      for e in (x[1], x[0], x[3], x[2]): l.add(e)
116                                          enf.sub(x,y).makeString())      enf = maker.makeEnfilade(l)
117        assert enf.makeString() == "feng shui"
118      def testFromList(self):      assert enf.getList().size() == 4
119          x = [self.makeSpan(e) for e in ("ng ", "fe", "i", "shu")]  
120          l = ArrayList()      #l = ArrayList()
121          for e in (x[1], x[0], x[3], x[2]): l.add(e)      #for e in (makeSpan("this "), enf, makeSpan(" is")): l.add(e)
122          enf = self.maker.makeEnfilade(l)      #enf = maker.makeEnfilade(l)
123          assert enf.makeString() == "feng shui"      #failUnlessEqual(enf.makeString(), "this feng shui is")
124          assert enf.getList().size() == 4      #assert enf.getList().size() == 7
125    
126          #l = ArrayList()  
127          #for e in (self.makeSpan("this "), enf, self.makeSpan(" is")): l.add(e)  
128          #enf = self.maker.makeEnfilade(l)  
129          #self.failUnlessEqual(enf.makeString(), "this feng shui is")  def testSerialization():
130          #assert enf.getList().size() == 7      "Test that enfilades' serialization works right."
131        s, sp, e, ep = bigEnf()
132        enf = e[-1]
133        str = s[-1]
134    
135      def testSerialization(self):      serv = SimpleMediaserver(TransientStorer(), IDSpace(), 0)
136          "Test that enfilades' serialization works right."  
137          s, sp, e, ep = self.bigEnf()      ous = io.ByteArrayOutputStream()
138          enf = e[-1]      MediaOutputStream(ous, serv).writeObject(enf)
139          str = s[-1]      ins = io.ByteArrayInputStream(ous.toByteArray())
140        obj = MediaInputStream(ins, serv).readObject()
141          serv = SimpleMediaserver(TransientStorer(), IDSpace(), 0)  
142        failUnlessEqual(str, obj.makeString())
143          ous = io.ByteArrayOutputStream()      failUnlessEqual(enf.getList(), obj.getList())
144          MediaOutputStream(ous, serv).writeObject(enf)      failUnlessEqual(enf, obj)
145          ins = io.ByteArrayInputStream(ous.toByteArray())      failUnlessEqual(enf.hashCode(), obj.hashCode())
         obj = MediaInputStream(ins, serv).readObject()  
   
         self.failUnlessEqual(str, obj.makeString())  
         self.failUnlessEqual(enf.getList(), obj.getList())  
         self.failUnlessEqual(enf, obj)  
         self.failUnlessEqual(enf.hashCode(), obj.hashCode())  
146    
147    
148      # XXX testIterator!      # XXX testIterator!
149    
150  class TestNonFakeEnfilade(TestEnfilade):  # Test this only if not using fake spans.
151      def testOptimizeJoins(self):  if not fake:
152        def testOptimizeJoins():
153          """Test that optimization joins adjacent spans"""          """Test that optimization joins adjacent spans"""
154          x = [self.makeSpan(e) for e in "foobar"]          x = [makeSpan(e) for e in "foobar"]
155          l = ArrayList()          l = ArrayList()
156          for e in "foobar": l.add(self.makeSpan(e))          for e in "foobar": l.add(makeSpan(e))
157          enf = self.maker.makeEnfilade(l).optimized()          enf = maker.makeEnfilade(l).optimized()
158          assert enf.makeString() == "foobar"          assert enf.makeString() == "foobar"
159          #print enf          #print enf
160          assert enf.getList().size() == 1          assert enf.getList().size() == 1

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26