2017-07-23 23:03:21 +02:00
|
|
|
# encoding: UTF-8
|
2017-02-12 13:21:08 +01:00
|
|
|
|
2017-07-23 23:03:21 +02:00
|
|
|
# KLayout Layout Viewer
|
2019-01-08 00:58:45 +01:00
|
|
|
# Copyright (C) 2006-2019 Matthias Koefferlein
|
2017-07-23 23:03:21 +02:00
|
|
|
#
|
|
|
|
|
# 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
|
2017-02-12 13:21:08 +01:00
|
|
|
|
|
|
|
|
load("test_prologue.rb")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Tl_TestClass < TestBase
|
|
|
|
|
|
|
|
|
|
# Expression basics
|
|
|
|
|
def test_1_Expression
|
|
|
|
|
|
2019-07-28 01:33:30 +02:00
|
|
|
ctx = RBA::ExpressionContext::new
|
|
|
|
|
assert_equal(ctx.eval("1+2"), 3)
|
|
|
|
|
ctx.var("a", 21)
|
|
|
|
|
assert_equal(ctx.eval("2*a"), 42)
|
|
|
|
|
|
2017-02-12 13:21:08 +01:00
|
|
|
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")
|
2018-06-24 21:26:59 +02:00
|
|
|
assert_equal(res.inspect, ["a", nil, 17, {8=>"x", "u"=>42}, [2, 7]].inspect)
|
2017-02-12 13:21:08 +01:00
|
|
|
|
|
|
|
|
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")
|
2018-06-24 21:26:59 +02:00
|
|
|
assert_equal(res.inspect, {8=>"x", "u"=>42}.inspect)
|
2017-02-12 13:21:08 +01:00
|
|
|
|
|
|
|
|
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.inspect, "[(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.inspect, "[(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.inspect, "[(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.inspect, "[(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.inspect, "[(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
|
|
|
|
|
|
2019-05-11 22:35:50 +02:00
|
|
|
# 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
|
|
|
|
|
|
2019-08-25 18:03:27 +02:00
|
|
|
class MyRecipe < RBA::Recipe
|
|
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
|
super("test_recipe", "description")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def execute(params)
|
|
|
|
|
a = params["A"] || 0
|
|
|
|
|
b = params["B"] || 0.0
|
|
|
|
|
b * a
|
|
|
|
|
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, "test_recipe")
|
|
|
|
|
assert_equal(my_recipe.description, "description")
|
|
|
|
|
|
|
|
|
|
g = my_recipe.generator("A" => 6, "B" => 7.0)
|
|
|
|
|
assert_equal(g, "test_recipe: A=#6,B=##7")
|
|
|
|
|
assert_equal("%g" % RBA::Recipe::make(g).to_s, "42")
|
|
|
|
|
|
|
|
|
|
my_recipe._destroy
|
|
|
|
|
my_recipe = nil
|
|
|
|
|
GC.start
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2017-02-12 13:21:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
load("test_epilogue.rb")
|