blob: 20903d81b2cfba28fe09d52bdbc3f6334de37bda [file] [log] [blame]
Ozan Can Altioke46419b2018-03-20 15:02:28 +03001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
20import unittest
21
22from DoubleConstantsTest import constants
23
24#
25# In order to run the test under Windows. We need to create symbolic link
26# name 'thrift' to '../src' folder by using:
27#
28# mklink /D thrift ..\src
29#
30
31
32class TestRenderedDoubleConstants(unittest.TestCase):
33 ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST = \
34 "failed to verify a double constant generated by Thrift (expected = %f, got = %f)"
35 ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_LIST_TEST =\
36 "failed to verify a list item by Thrift (expected = %f, got = %f)"
37 ASSERTION_MESSAGE_FOR_TYPE_CHECKS = "the rendered variable with name %s is not of double type"
38
39 # to make sure the variables inside Thrift files are generated correctly
40 def test_rendered_double_constants(self):
41 EXPECTED_DOUBLE_ASSIGNED_TO_INT_CONSTANT = 1.0
42 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT = -100.0
43 EXPECTED_DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT = 9223372036854775807.0
44 EXPECTED_DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT = -9223372036854775807.0
45 EXPECTED_DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS = 3.14159265359
46 EXPECTED_DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE = 1000000.1
47 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE = -1000000.1
48 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_DOUBLE = 1.7e+308
49 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE = 9223372036854775816.43
50 EXPECTED_DOUBLE_ASSIGNED_TO_SMALL_DOUBLE = -1.7e+308
51 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE = -9223372036854775816.43
52 self.assertAlmostEqual(
53 constants.DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_INT_CONSTANT, places=7,
54 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
55 EXPECTED_DOUBLE_ASSIGNED_TO_INT_CONSTANT, constants.DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST))
56 self.assertAlmostEqual(
57 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT,
58 places=7,
59 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
60 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT,
61 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST))
62 self.assertAlmostEqual(
63 constants.DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT,
64 places=7,
65 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
66 EXPECTED_DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT,
67 constants.DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST))
68 self.assertAlmostEqual(
69 constants.DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT,
70 places=7,
71 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
72 EXPECTED_DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT,
73 constants.DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST))
74 self.assertAlmostEqual(
75 constants.DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST,
76 EXPECTED_DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS, places=7,
77 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
78 EXPECTED_DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS,
79 constants.DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST))
80 self.assertAlmostEqual(
81 constants.DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE,
82 places=7,
83 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
84 EXPECTED_DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE,
85 constants.DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST))
86 self.assertAlmostEqual(
87 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST,
88 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE, places=7,
89 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
90 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE,
91 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST))
92 self.assertAlmostEqual(
93 constants.DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_DOUBLE, places=7,
94 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
95 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_DOUBLE,
96 constants.DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST))
97 self.assertAlmostEqual(
98 constants.DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST,
99 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE, places=7,
100 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
101 EXPECTED_DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE,
102 constants.DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST))
103 self.assertAlmostEqual(
104 constants.DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST, EXPECTED_DOUBLE_ASSIGNED_TO_SMALL_DOUBLE, places=7,
105 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
106 EXPECTED_DOUBLE_ASSIGNED_TO_SMALL_DOUBLE,
107 constants.DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST))
108 self.assertAlmostEqual(
109 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST,
110 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE, places=7,
111 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_RENDERED_DOUBLE_CONSTANTS_TEST % (
112 EXPECTED_DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE,
113 constants.DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST))
114 self.assertTrue(
115 isinstance(constants.DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST, float),
116 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
117 "DOUBLE_ASSIGNED_TO_INT_CONSTANT_TEST")
118 self.assertTrue(
119 isinstance(constants.DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST, float),
120 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
121 "DOUBLE_ASSIGNED_TO_NEGATIVE_INT_CONSTANT_TEST")
122 self.assertTrue(
123 isinstance(constants.DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST, float),
124 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
125 "DOUBLE_ASSIGNED_TO_LARGEST_INT_CONSTANT_TEST")
126 self.assertTrue(
127 isinstance(constants.DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST, float),
128 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
129 "DOUBLE_ASSIGNED_TO_SMALLEST_INT_CONSTANT_TEST")
130 self.assertTrue(
131 isinstance(constants.DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST, float),
132 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
133 "DOUBLE_ASSIGNED_TO_DOUBLE_WITH_MANY_DECIMALS_TEST")
134 self.assertTrue(
135 isinstance(constants.DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST, float),
136 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
137 "DOUBLE_ASSIGNED_TO_FRACTIONAL_DOUBLE_TEST")
138 self.assertTrue(
139 isinstance(constants.DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST, float),
140 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
141 "DOUBLE_ASSIGNED_TO_NEGATIVE_FRACTIONAL_DOUBLE_TEST")
142 self.assertTrue(
143 isinstance(constants.DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST, float),
144 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
145 "DOUBLE_ASSIGNED_TO_LARGE_DOUBLE_TEST")
146 self.assertTrue(
147 isinstance(constants.DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST, float),
148 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
149 "DOUBLE_ASSIGNED_TO_LARGE_FRACTIONAL_DOUBLE_TEST")
150 self.assertTrue(
151 isinstance(constants.DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST, float),
152 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
153 "DOUBLE_ASSIGNED_TO_SMALL_DOUBLE_TEST")
154 self.assertTrue(
155 isinstance(constants.DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST, float),
156 msg=TestRenderedDoubleConstants.ASSERTION_MESSAGE_FOR_TYPE_CHECKS %
157 "DOUBLE_ASSIGNED_TO_NEGATIVE_BUT_LARGE_FRACTIONAL_DOUBLE_TEST")
158
159 # to make sure the variables inside Thrift files are generated correctly
160 def test_rendered_double_list(self):
161 EXPECTED_DOUBLE_LIST = [1.0, -100.0, 100.0, 9223372036854775807.0, -9223372036854775807.0, 3.14159265359,
162 1000000.1, -1000000.1, 1.7e+308, -1.7e+308, 9223372036854775816.43,
163 -9223372036854775816.43]
164 self.assertEqual(len(constants.DOUBLE_LIST_TEST), len(EXPECTED_DOUBLE_LIST))
165 for i, expectedValue in enumerate(EXPECTED_DOUBLE_LIST):
166 self.assertAlmostEqual(constants.DOUBLE_LIST_TEST[i], expectedValue, places=7)
167
168
169def suite():
170 suite = unittest.TestSuite()
171 loader = unittest.TestLoader()
172 suite.addTest(loader.loadTestsFromTestCase(TestRenderedDoubleConstants))
173 return suite
174
175
176if __name__ == "__main__":
177 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))