1#!/usr/bin/env python2
2#
3# Copyright (C) 2014 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from common.immutables               import ImmutableDict
18from common.testing                  import ToUnicode
19from file_format.c1visualizer.parser import ParseC1visualizerStream
20from file_format.c1visualizer.struct import C1visualizerFile, C1visualizerPass
21
22import io
23import unittest
24
25class C1visualizerParser_Test(unittest.TestCase):
26
27  def createFile(self, data):
28    """ Creates an instance of CheckerFile from provided info.
29
30    Data format: ( [ <isa-feature>, ... ],
31                   [ ( <case-name>, [ ( <text>, <assert-variant> ), ... ] ), ... ]
32                 )
33    """
34    c1File = C1visualizerFile("<c1_file>")
35    c1File.instructionSetFeatures = data[0]
36    for passEntry in data[1]:
37      passName = passEntry[0]
38      passBody = passEntry[1]
39      c1Pass = C1visualizerPass(c1File, passName, passBody, 0)
40    return c1File
41
42  def assertParsesTo(self, c1Text, expectedData):
43    expectedFile = self.createFile(expectedData)
44    actualFile = ParseC1visualizerStream("<c1_file>", io.StringIO(ToUnicode(c1Text)))
45    return self.assertEqual(expectedFile, actualFile)
46
47  def test_EmptyFile(self):
48    self.assertParsesTo("", (ImmutableDict(), []))
49
50  def test_SingleGroup(self):
51    self.assertParsesTo(
52      """
53        begin_compilation
54          method "MyMethod"
55        end_compilation
56        begin_cfg
57          name "pass1"
58          foo
59          bar
60        end_cfg
61      """,
62      ( ImmutableDict(), [
63        ( "MyMethod pass1", [ "foo", "bar" ] )
64      ]))
65
66  def test_MultipleGroups(self):
67    self.assertParsesTo(
68      """
69        begin_compilation
70          name "xyz1"
71          method "MyMethod1"
72          date 1234
73        end_compilation
74        begin_cfg
75          name "pass1"
76          foo
77          bar
78        end_cfg
79        begin_cfg
80          name "pass2"
81          abc
82          def
83        end_cfg
84      """,
85      ( ImmutableDict(), [
86        ( "MyMethod1 pass1", [ "foo", "bar" ] ),
87        ( "MyMethod1 pass2", [ "abc", "def" ] )
88      ]))
89    self.assertParsesTo(
90      """
91        begin_compilation
92          name "xyz1"
93          method "MyMethod1"
94          date 1234
95        end_compilation
96        begin_cfg
97          name "pass1"
98          foo
99          bar
100        end_cfg
101        begin_compilation
102          name "xyz2"
103          method "MyMethod2"
104          date 5678
105        end_compilation
106        begin_cfg
107          name "pass2"
108          abc
109          def
110        end_cfg
111      """,
112      ( ImmutableDict(), [
113        ( "MyMethod1 pass1", [ "foo", "bar" ] ),
114        ( "MyMethod2 pass2", [ "abc", "def" ] )
115      ]))
116
117  def test_InstructionSetFeatures(self):
118    self.assertParsesTo(
119      """
120        begin_compilation
121          name "isa_features:feature1,-feature2"
122          method "isa_features:feature1,-feature2"
123          date 1234
124        end_compilation
125      """,
126      ( ImmutableDict({"feature1": True, "feature2": False}), []))
127    self.assertParsesTo(
128      """
129        begin_compilation
130          name "isa_features:feature1,-feature2"
131          method "isa_features:feature1,-feature2"
132          date 1234
133        end_compilation
134        begin_compilation
135          name "xyz1"
136          method "MyMethod1"
137          date 1234
138        end_compilation
139        begin_cfg
140          name "pass1"
141          foo
142          bar
143        end_cfg
144      """,
145      ( ImmutableDict({"feature1": True, "feature2": False}), [
146        ( "MyMethod1 pass1", [ "foo", "bar" ] )
147      ]))
148    self.assertParsesTo(
149      """
150        begin_compilation
151          name "isa:some_isa isa_features:feature1,-feature2"
152          method "isa:some_isa isa_features:feature1,-feature2"
153          date 1234
154        end_compilation
155      """,
156      ( ImmutableDict({"feature1": True, "feature2": False}), []))
157    self.assertParsesTo(
158      """
159        begin_compilation
160          name "isa:some_isa isa_features:feature1,-feature2"
161          method "isa:some_isa isa_features:feature1,-feature2"
162          date 1234
163        end_compilation
164        begin_compilation
165          name "xyz1"
166          method "MyMethod1"
167          date 1234
168        end_compilation
169        begin_cfg
170          name "pass1"
171          foo
172          bar
173        end_cfg
174      """,
175      ( ImmutableDict({"feature1": True, "feature2": False}), [
176        ( "MyMethod1 pass1", [ "foo", "bar" ] )
177      ]))
178