• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14require 'stringio'
15
16module Generator
17  class TestBase
18    def initialize(isa, command, definitions, predefined, skip_header)
19      @isa = isa
20      @command = command
21      @predefined = predefined
22      @definitions = definitions
23      @skip_header = skip_header
24    end
25
26    def write_test_initial_block(content)
27      if !@skip_header
28        isa = @isa.clone
29        isa.merge! @command[Generator::TEST_COMMAND_ISA]
30        # Currently generator supports only single instruction per test. However coverage tool supports array.
31        # So convert to yaml as array of single element.
32
33        content.puts '# Huawei Technologies Co.,Ltd.'
34        content.puts '#'
35        content.puts '# The following comment until the empty line must be a valid YAML document'
36        content.puts "# containing exact copies of ISA specification assertions relevant to this test.\n"
37        content.puts [isa].to_yaml.split("\n").map { |line| '#%s' % line }.join("\n")
38        content.puts "\n"
39      end
40    end
41
42    def write_runner_options(content, run_options, ignore, bugids, tags, description, test_panda_options)
43      if !@skip_header
44        run_options.each do |s|
45          content.puts "## runner-option: #{s}"
46        end
47        content.puts "## runner-option: ignore" if ignore
48        content.puts "## runner-option: bugid: #{bugids.join ', '}" if bugids.length > 0
49        tags.append 'ignore' if ignore
50        content.puts "## runner-option: tags: #{tags.join ', '}" if tags.length > 0
51        content.puts "## panda-options: #{test_panda_options}" if test_panda_options.length > 0
52
53        # Options for test with main wrapper function to avoid false-positive cases, for executable tests
54        if !(run_options.include?('compile-failure') || run_options.include?('compile-only') ||
55             run_options.include?('verifier-failure') || run_options.include?('verifier-only'))
56          content.puts "## runner-option: main-exitcode-wrapper"
57        end
58
59        if description != ""
60          content.puts "\n# Test description:"
61          description.split("\n").each {|t| content.puts "\#   #{t}" }
62          content.puts ""
63        end
64      end
65    end
66
67    def get_header_template()
68      @command[Generator::TEST_HEADER_TEMPLATE]
69    end
70
71    def write_test_main_block(content)
72      # Mandatory empty line after header
73      ## TEST_HEADER_TEMPLATE
74
75      content.puts "\n"
76      header_template = get_header_template
77      if header_template != nil
78        header_template.each do |template|
79          if @definitions.exist?(template)
80            content.puts @definitions.definition template
81          else
82            content.puts @predefined.definition template
83          end
84        end
85      else
86        content.puts @predefined.definition Generator::DEF_MAIN
87      end
88    end
89
90    def write_test_main_wrapper_block(content, run_options)
91      # Add main wrapper to executable tests
92      if !(run_options.include?('compile-failure') || run_options.include?('compile-only') ||
93           run_options.include?('verifier-failure') || run_options.include?('verifier-only'))
94        content.puts @predefined.definition 'main-exitcode-wrapper'
95      end
96    end
97
98  end
99end
100