• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ConstantFoldingOverflow_test.cpp:
7 //   Tests for constant folding that results in floating point overflow.
8 //   In IEEE floating point, the overflow result depends on which of the various rounding modes is
9 //   chosen - it's either the maximum representable value or infinity.
10 //   ESSL 3.00.6 section 4.5.1 says that the rounding mode cannot be set and is undefined, so the
11 //   result in this case is not defined by the spec.
12 //   We decide to overflow to infinity and issue a warning.
13 //
14 
15 #include "tests/test_utils/ConstantFoldingTest.h"
16 
17 using namespace sh;
18 
19 namespace
20 {
21 
22 class ConstantFoldingOverflowExpressionTest : public ConstantFoldingExpressionTest
23 {
24   public:
ConstantFoldingOverflowExpressionTest()25     ConstantFoldingOverflowExpressionTest() {}
26 
evaluateFloatOverflow(const std::string & floatString,bool positive)27     void evaluateFloatOverflow(const std::string &floatString, bool positive)
28     {
29         evaluateFloat(floatString);
30         float expected = positive ? std::numeric_limits<float>::infinity()
31                                   : -std::numeric_limits<float>::infinity();
32         ASSERT_TRUE(constantFoundInAST(expected));
33         ASSERT_TRUE(hasWarning());
34     }
35 };
36 
37 }  // anonymous namespace
38 
39 // Test that addition that overflows is evaluated correctly.
TEST_F(ConstantFoldingOverflowExpressionTest,Add)40 TEST_F(ConstantFoldingOverflowExpressionTest, Add)
41 {
42     const std::string &floatString = "2.0e38 + 2.0e38";
43     evaluateFloatOverflow(floatString, true);
44 }
45 
46 // Test that subtraction that overflows is evaluated correctly.
TEST_F(ConstantFoldingOverflowExpressionTest,Subtract)47 TEST_F(ConstantFoldingOverflowExpressionTest, Subtract)
48 {
49     const std::string &floatString = "2.0e38 - (-2.0e38)";
50     evaluateFloatOverflow(floatString, true);
51 }
52 
53 // Test that multiplication that overflows is evaluated correctly.
TEST_F(ConstantFoldingOverflowExpressionTest,Multiply)54 TEST_F(ConstantFoldingOverflowExpressionTest, Multiply)
55 {
56     const std::string &floatString = "1.0e30 * 1.0e10";
57     evaluateFloatOverflow(floatString, true);
58 }
59 
60 // Test that division that overflows is evaluated correctly.
TEST_F(ConstantFoldingOverflowExpressionTest,Divide)61 TEST_F(ConstantFoldingOverflowExpressionTest, Divide)
62 {
63     const std::string &floatString = "1.0e30 / 1.0e-10";
64     evaluateFloatOverflow(floatString, true);
65 }
66