File: tlTest.rb

package info (click to toggle)
klayout 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 292,204 kB
  • sloc: cpp: 2,068,428; ruby: 47,823; xml: 26,924; python: 14,404; sh: 1,812; tcl: 212; perl: 170; makefile: 112; ansic: 42
file content (317 lines) | stat: -rw-r--r-- 10,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# encoding: UTF-8

# KLayout Layout Viewer
# Copyright (C) 2006-2025 Matthias Koefferlein
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

if !$:.member?(File::dirname($0))
  $:.push(File::dirname($0))
end

load("test_prologue.rb")


class Tl_TestClass < TestBase

  # Expression basics
  def test_1_Expression

    ctx = RBA::ExpressionContext::new
    assert_equal(ctx.eval("1+2"), 3)
    ctx.var("a", 21)
    assert_equal(ctx.eval("2*a"), 42)

    expr = RBA::Expression::new
    res = expr.eval
    assert_equal(res.class.to_s, "NilClass")
    assert_equal(res.to_s, "")

    expr = RBA::Expression.eval("1+2")
    assert_equal(expr.class.to_s, "Float")
    assert_equal(expr.to_s, "3.0")

    expr = RBA::Expression::new
    expr.text = "1+2"
    res = expr.eval
    assert_equal(res.class.to_s, "Float")
    assert_equal(res.to_s, "3.0")

    expr = RBA::Expression::new
    expr.var("a", 5)
    expr.text = "a+to_i(2)"
    res = expr.eval
    assert_equal(res.class.to_s == "Fixnum" || res.class.to_s == "Integer", true)
    assert_equal(res.to_s, "7")
    expr.var("a", 7)
    res = expr.eval
    assert_equal(res.to_s, "9")

    RBA::Expression::global_var("xxx", 17.5)
    expr = RBA::Expression::new("xxx+1")
    res = expr.eval
    assert_equal(res.class.to_s, "Float")
    assert_equal(res.to_s, "18.5")

    expr = RBA::Expression::new("a+b*2", { "a" => 18, "b" => 2.5 })
    res = expr.eval
    assert_equal(res.class.to_s, "Float")
    assert_equal(res.to_s, "23.0")

    expr = RBA::Expression::new("[a[1],a[2],a[0],a[4],a[3]]", { "a" => [ 17, "a", nil, [ 2, 7 ], { 8 => "x", "u" => 42 } ] })
    res = expr.eval
    assert_equal(res.class.to_s, "Array")
    assert_equal(res.inspect, ["a", nil, 17, {8=>"x", "u"=>42}, [2, 7]].inspect)

    expr = RBA::Expression::new("a[1]", { "a" => [ 17, "a", nil, [ 2, 7 ], { 8 => "x", "u" => 42 } ] })
    res = expr.eval
    assert_equal(res.class.to_s, "String")
    assert_equal(res.to_s, "a")

    expr = RBA::Expression::new("a[4]", { "a" => [ 17, "a", nil, [ 2, 7 ], { 8 => "x", "u" => 42 } ] })
    res = expr.eval
    assert_equal(res.class.to_s, "Hash")
    assert_equal(res.inspect, {8=>"x", "u"=>42}.inspect)

  end

  # Advanced expressions 
  def test_2_Expression

    box1 = RBA::Box::new(0, 100, 200, 300)
    box2 = RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("a", { "a" => box1, "b" => box2 })
    res = expr.eval

    assert_equal(res.to_s, "(0,100;200,300)")

    # boxes are non-managed objects -> passing the object through the expression does not persist their ID
    assert_not_equal(res.object_id, box1.object_id)
    assert_not_equal(res.object_id, box2.object_id)

    # -------------------------------------------------

    box1 = RBA::Box::new(0, 100, 200, 300)
    box2 = RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("a&b", { "a" => box1, "b" => box2 })
    res = expr.eval

    assert_equal(res.to_s, "(50,150;200,300)")

    # computed objects are entirely new ones
    assert_not_equal(res.object_id, box1.object_id)
    assert_not_equal(res.object_id, box2.object_id)

    # -------------------------------------------------

    box1 = RBA::Box::new(0, 100, 200, 300)
    box2 = RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("x=a&b; y=x; z=y; [x,y,z]", { "a" => box1, "b" => box2, "x" => nil, "y" => nil, "z" => nil })
    res = expr.eval

    assert_equal(res.map(&:to_s), %w((50,150;200,300) (50,150;200,300) (50,150;200,300)))

    # all objects are individual copies
    assert_not_equal(res[0].object_id, box1.object_id)
    assert_not_equal(res[0].object_id, box2.object_id)
    assert_not_equal(res[1].object_id, res[0].object_id)
    assert_not_equal(res[2].object_id, res[0].object_id)

    # -------------------------------------------------

    box1 = RBA::Box::new(0, 100, 200, 300)
    box2 = RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("var x=a&b; var y=x; var z=y; [x,y,z]", { "a" => box1, "b" => box2 })
    res = expr.eval

    assert_equal(res.map(&:to_s), %w((50,150;200,300) (50,150;200,300) (50,150;200,300)))

    # all objects are individual copies
    assert_not_equal(res[0].object_id, box1.object_id)
    assert_not_equal(res[0].object_id, box2.object_id)
    assert_not_equal(res[1].object_id, res[0].object_id)
    assert_not_equal(res[2].object_id, res[0].object_id)

    # destruction of the expression's object space does not matter since we have copies
    expr._destroy
    assert_equal(res.map(&:to_s), %w((50,150;200,300) (50,150;200,300) (50,150;200,300)))

    # -------------------------------------------------

    region1 = RBA::Region::new
    region1 |= RBA::Box::new(0, 100, 200, 300)
    region2 = RBA::Region::new
    region2 |= RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("a", { "a" => region1, "b" => region2 })
    res = expr.eval

    # regions are managed objects -> passing the object through the expression persists it's object ID
    assert_equal(res.object_id, region1.object_id)
    assert_not_equal(res.object_id, region2.object_id)

    # -------------------------------------------------

    region1 = RBA::Region::new
    region1 |= RBA::Box::new(0, 100, 200, 300)
    region2 = RBA::Region::new
    region2 |= RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("a&b", { "a" => region1, "b" => region2, "x" => nil, "y" => nil, "z" => nil })
    res = expr.eval

    assert_equal(res.to_s, "(50,150;50,300;200,300;200,150)")

    # The returned object (as a new one) is an entirely fresh one
    assert_not_equal(res.object_id, region1.object_id)
    assert_not_equal(res.object_id, region2.object_id)

    # -------------------------------------------------

    region1 = RBA::Region::new
    region1 |= RBA::Box::new(0, 100, 200, 300)
    region2 = RBA::Region::new
    region2 |= RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("x=a&b; y=x; z=y; [x,y,z]", { "a" => region1, "b" => region2, "x" => nil, "y" => nil, "z" => nil })
    res = expr.eval

    assert_equal(res.map(&:to_s), %w((50,150;50,300;200,300;200,150) (50,150;50,300;200,300;200,150) (50,150;50,300;200,300;200,150)))

    # regions are managed objects -> passing the object through the expression persists it's object ID
    assert_not_equal(res[0].object_id, region1.object_id)
    assert_not_equal(res[0].object_id, region2.object_id)
    assert_equal(res[1].object_id, res[0].object_id)
    assert_equal(res[2].object_id, res[0].object_id)

    # -------------------------------------------------

    region1 = RBA::Region::new
    region1 |= RBA::Box::new(0, 100, 200, 300)
    region2 = RBA::Region::new
    region2 |= RBA::Box::new(50, 150, 250, 350)
    expr = RBA::Expression::new("var x=a&b; var y=x; var z=y; [x,y,z]", { "a" => region1, "b" => region2 })
    res = expr.eval

    assert_equal(res.map(&:to_s), %w((50,150;50,300;200,300;200,150) (50,150;50,300;200,300;200,150) (50,150;50,300;200,300;200,150)))

    # regions are managed objects -> passing the object through the expression persists it's object ID
    assert_not_equal(res[0].object_id, region1.object_id)
    assert_not_equal(res[0].object_id, region2.object_id)
    assert_equal(res[1].object_id, res[0].object_id)
    assert_equal(res[2].object_id, res[0].object_id)

    # the result objects live in the expression object space and are destroyed with the expression
    expr._destroy

    assert_equal(res.size, 3)
    assert_equal(res[0].destroyed?, true)
    assert_equal(res[1].destroyed?, true)
    assert_equal(res[2].destroyed?, true)

  end

  # Glob pattern
  def test_3_GlobPattern

    pat = RBA::GlobPattern::new("a*b")

    assert_equal(pat.case_sensitive, true)
    assert_equal(pat.head_match, false)

    assert_equal(pat.match("ab") != nil, true)
    assert_equal(pat.match("axb") != nil, true)
    assert_equal(pat.match("Axb") != nil, false)
    assert_equal(pat.match("abx") != nil, false)
    assert_equal(pat.match("xab") != nil, false)

    pat.case_sensitive = false
    assert_equal(pat.case_sensitive, false)

    assert_equal(pat.match("ab") != nil, true)
    assert_equal(pat.match("axb") != nil, true)
    assert_equal(pat.match("Axb") != nil, true)
    assert_equal(pat.match("abx") != nil, false)
    assert_equal(pat.match("xab") != nil, false)

    pat.head_match = true
    assert_equal(pat.head_match, true)

    assert_equal(pat.match("ab") != nil, true)
    assert_equal(pat.match("axb") != nil, true)
    assert_equal(pat.match("Axb") != nil, true)
    assert_equal(pat.match("abx") != nil, true)
    assert_equal(pat.match("abx") == [], true)
    assert_equal(pat.match("xab") != nil, false)

    pat = RBA::GlobPattern::new("(*)a(*)")

    assert_equal(pat.match("xb") != nil, false)
    res = pat.match("xab")
    assert_equal(res != nil, true)
    assert_equal(res.join("/"), "x/b")

  end

  class MyRecipeExecutable < RBA::Executable

    def initialize(params)
      @params = params
    end

    def execute
      a = @params["A"] || 0
      b = @params["B"] || 0.0
      c = @params["C"] || 1.0
      b * a * c
    end

  end

  class MyRecipe < RBA::Recipe

    def initialize
      super("rba_test_recipe", "description")
    end

    def executable(params)
      return MyRecipeExecutable::new(params)
    end

  end

  # Recipe
  def test_4_Recipe

    # make sure there isn't a second instance
    GC.start

    my_recipe = MyRecipe::new
    my_recipe._create # makes debugging easier

    assert_equal(my_recipe.name, "rba_test_recipe")
    assert_equal(my_recipe.description, "description")

    g = my_recipe.generator("A" => 6, "B" => 7.0)
    assert_equal(g, "rba_test_recipe: A=#6,B=##7")
    assert_equal("%g" % RBA::Recipe::make(g), "42")
    assert_equal("%g" % RBA::Recipe::make(g, "C" => 1.5).to_s, "63")

    my_recipe._destroy
    my_recipe = nil
    GC.start

  end

end

load("test_epilogue.rb")