1#!/usr/bin/env ruby 2 3# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16Options = Object.new 17class << Options 18 def arch 19 :x86_64 20 end 21end 22 23require_relative '../regmask' 24require 'test/unit' 25 26class RegMaskTest < Test::Unit::TestCase 27 def setup 28 # Do nothing 29 end 30 31 def teardown 32 # Do nothing 33 end 34 35 def test_regmap 36 r1 = Regmap.new({ arm64: {a: 1}, arm32: {a: 2}, x86_64: {a: 3} }) 37 r2 = Regmap.new({ arm64: {b: 4}, arm32: {b: 5}, x86_64: {b: 6} }) 38 39 r3 = r1 + r2 40 assert_equal(r3, Regmap.new({ arm64: {a: 1, b: 4}, arm32: {a: 2, b: 5}, x86_64: {a: 3, b: 6} })) 41 r4 = r3 - r1 42 assert_equal(r4, r2) 43 r5 = r3 + {c: 7} 44 assert_equal(r5, {a: 3, b: 6, c: 7}) 45 r6 = r5 - {a: 2, c: 7} 46 assert_equal(r6, {b: 6}) 47 48 # Added Regmap should overwrite keys with the same name 49 r7 = r1 + {a: 11} 50 assert_equal(r7, {a: 11}) 51 end 52 53 def test_regmask 54 assert_raise(RuntimeError) { RegMask.new('a') } 55 assert_raise(RuntimeError) { RegMask.new(:a) } 56 57 map = Regmap.from_hash({a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}) 58 mask = RegMask.new(map, :a, :b, :c, :d, :e, :f) 59 m1 = RegMask.new(map[:b], map[:d]) 60 m2 = RegMask.new(map, :b, :d) 61 assert_equal(m1, m2) 62 m3 = RegMask.new(map, :a, :c, :e) 63 m4 = m1 + m3 64 assert_equal(m4, RegMask.new(map, :a, :b, :c, :d, :e)) 65 assert_equal(m4 - m1, m3) 66 assert_equal(m3 - m3, RegMask.new()) 67 assert_equal(RegMask.new(map, :a) + map[:b], RegMask.new(map, :a, :b)) 68 assert_equal(RegMask.new(map, :a, :b) - map[:b], RegMask.new(map, :a)) 69 70 # Symbols addition/substraction 71 assert_equal(RegMask.new(map, :b, :d) + :a + :c + :e + :f, mask) 72 assert_equal(mask - :c - :e - :f, RegMask.new(map, :a, :b, :d)) 73 74 # Regmap addition/substraction 75 assert_equal(RegMask.new(map, :b, :d) + Regmap.from_hash({a: 1, e: 5}), RegMask.new(map, :a, :b, :d, :e)) 76 assert_equal(mask - Regmap.from_hash({a: 1, e: 5}), RegMask.new(map, :b, :c, :d, :f)) 77 assert_equal(mask - map, RegMask.new(map)) 78 79 # Symbol is not in the regmap 80 assert_raise(RuntimeError) { RegMask.new(map, :aaa) } 81 82 # Test `each` method and [] opertors 83 m = RegMask.new(map, :b, :d) 84 arr = [] 85 m.each {|x| arr << x} 86 assert_equal(arr, [2, 4]) 87 m[1] = true 88 assert_equal(m, RegMask.new(map, :a, :b, :d)) 89 assert_true(m[:a]) 90 assert_true(m[1]) 91 m[:c] = true 92 assert_equal(m, RegMask.new(map, :a, :b, :c, :d)) 93 assert_true(m[:c]) 94 assert_true(m[:b]) 95 m[:b] = false 96 assert_false(m[:b]) 97 assert_equal(m, RegMask.new(map, :a, :c, :d)) 98 assert_true(m[1]) 99 m[1] = false 100 assert_false(m[1]) 101 assert_equal(m, RegMask.new(map, :c, :d)) 102 end 103end