2 # Copyright (C) 2013 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 from make_token_matcher
import BadInput
, CaseLineProcessor
, MainLineProcessor
, Optimizer
, process_file
, SwitchCase
, SwitchLineProcessor
36 class OptimizerTest(unittest
.TestCase
):
37 def test_nonalphabetic(self
):
38 optimizer
= Optimizer(None, None, None)
41 optimizer
.inspect_array
,
42 [SwitchCase('-', None), SwitchCase('x', None)],
46 class MainLineProcessorTest(unittest
.TestCase
):
47 def test_switch(self
):
48 processor
= MainLineProcessor(None)
49 switchLineProcessor
= processor
.process_line('SWITCH(array, length) {')
50 self
.assertIsInstance(switchLineProcessor
, SwitchLineProcessor
)
51 self
.assertEquals('array', switchLineProcessor
.array_variable
)
52 self
.assertEquals('length', switchLineProcessor
.length_variable
)
55 class SwitchLineProcessorTest(unittest
.TestCase
):
57 processor
= SwitchLineProcessor(None, None, None, None)
58 caseLineProcessor
= processor
.process_line('CASE("identifier") {')
59 self
.assertIsInstance(caseLineProcessor
, CaseLineProcessor
)
60 self
.assertEquals('identifier', caseLineProcessor
.identifier
)
62 def test_unexpected(self
):
63 processor
= SwitchLineProcessor(None, None, None, None)
66 processor
.process_line
,
69 def test_repeated(self
):
70 processor
= SwitchLineProcessor(None, None, None, None)
71 processor
.process_line('CASE("x") {').process_line('}')
72 caseLineProcessor
= processor
.process_line('CASE("x") {')
75 caseLineProcessor
.process_line
,
79 class CaseLineProcessorTest(unittest
.TestCase
):
81 processor
= CaseLineProcessor(None, None, None)
84 processor
.process_line
,
88 class ProcessFileTest(unittest
.TestCase
):
101 if (LIKELY(q == 2)) {
102 if (LIKELY(p[1] == '(')) {
103 if ((p[0] | 0x20) == 'a') {
105 } else if (LIKELY((p[0] | 0x20) == 'b')) {
126 EXPECTED_MEDIUM
= """
127 if (LIKELY(q == 2)) {
128 if ((p[1] | 0x20) == 'b') {
129 if (LIKELY((p[0] | 0x20) == 'a')) {
132 } else if (LIKELY((p[1] | 0x20) == 'd')) {
133 if ((p[0] | 0x20) == 'c') {
135 } else if (LIKELY((p[0] | 0x20) == 'e')) {
177 if (LIKELY((p[0] | 0x20) == 'a')) {
183 if (LIKELY(p[1] == '-')) {
184 switch ((p[0] | 0x20)) {
199 switch ((p[0] | 0x20)) {
201 if (LIKELY((p[1] | 0x20) == 'f' && (p[2] | 0x20) == 'g')) {
206 if (LIKELY((p[1] | 0x20) == 'i' && (p[2] | 0x20) == 'j')) {
211 if (LIKELY((p[1] | 0x20) == 'l' && (p[2] | 0x20) == 'm')) {
221 def validate(self
, source
, expected
):
222 with tempfile
.NamedTemporaryFile() as input_file
:
223 with tempfile
.NamedTemporaryFile() as generated_file
:
224 input_file
.write(source
)
226 process_file(input_file
.name
, generated_file
.name
)
227 # Our code generation does not yet implement pretty indentation.
228 actual
= generated_file
.read().replace(' ', '')
229 expected
= expected
.replace(' ', '')
230 self
.assertEquals(actual
, expected
)
232 def test_small(self
):
233 self
.validate(ProcessFileTest
.SOURCE_SMALL
, ProcessFileTest
.EXPECTED_SMALL
)
235 def test_medium(self
):
236 self
.validate(ProcessFileTest
.SOURCE_MEDIUM
, ProcessFileTest
.EXPECTED_MEDIUM
)
238 def test_large(self
):
239 self
.validate(ProcessFileTest
.SOURCE_LARGE
, ProcessFileTest
.EXPECTED_LARGE
)
242 if __name__
== "__main__":