Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2009-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file RGBColorTest.cpp
15 : /// @author Matthias Heppner
16 : /// @author Michael Behrisch
17 : /// @author Jakob Erdmann
18 : /// @date Sept 2009
19 : ///
20 : // Tests the class RGBColor
21 : /****************************************************************************/
22 :
23 :
24 : // ===========================================================================
25 : // included modules
26 : // ===========================================================================
27 : #include <config.h>
28 :
29 : #include <gtest/gtest.h>
30 : #include <utils/common/RGBColor.h>
31 : #include <utils/common/UtilExceptions.h>
32 : #include <iostream>
33 : #include <fstream>
34 : #include <sstream>
35 :
36 :
37 : // ===========================================================================
38 : // test definitions
39 : // ===========================================================================
40 : /* Test the method 'parseColor' with a wrong String. An exception must occur.*/
41 1 : TEST(RGBColor, test_parseColor_with_wrong_string) {
42 2 : ASSERT_THROW(RGBColor::parseColor("a,b,y"), NumberFormatException);
43 : }
44 :
45 : /* Test the method 'parseColor'*/
46 1 : TEST(RGBColor, test_parseColor) {
47 1 : RGBColor color = RGBColor::parseColor("0,1,255");
48 1 : EXPECT_EQ(0, color.red());
49 1 : EXPECT_EQ(1, color.green());
50 1 : EXPECT_EQ(255, color.blue());
51 :
52 1 : RGBColor color2 = RGBColor::parseColor("RED");
53 1 : EXPECT_EQ(255, color2.red());
54 1 : EXPECT_EQ(0, color2.green());
55 1 : EXPECT_EQ(0, color2.blue());
56 :
57 1 : }
58 :
59 : /* Test the method 'parseColor' with thrown EmptyData Exception*/
60 1 : TEST(RGBColor, test_parseColor_with_a_short_string) {
61 2 : ASSERT_THROW(RGBColor::parseColor(""), FormatException) << "Expected a FormatException.";
62 2 : ASSERT_THROW(RGBColor::parseColor("1,2"), FormatException) << "Expected a FormatException.";
63 2 : ASSERT_THROW(RGBColor::parseColor("test"), FormatException) << "Expected a FormatException.";
64 2 : ASSERT_THROW(RGBColor::parseColor("1,2,3,5,432test"), FormatException) << "Expected a FormatException.";
65 : }
66 :
67 : /* Test the method 'interpolate'*/
68 1 : TEST(RGBColor, test_interpolate) {
69 1 : RGBColor color1 = RGBColor(1, 2, 3);
70 1 : RGBColor color2 = RGBColor(3, 4, 1);
71 1 : RGBColor colorResult = RGBColor::interpolate(color1, color2, 0.5);
72 1 : EXPECT_EQ(2, colorResult.red());
73 1 : EXPECT_EQ(3, colorResult.green());
74 1 : EXPECT_EQ(2, colorResult.blue());
75 1 : }
76 :
77 : /* Test the method 'interpolate' with a weight of 1 and higher*/
78 1 : TEST(RGBColor, test_interpolate_weight_1) {
79 1 : RGBColor color1 = RGBColor(1, 2, 3);
80 1 : RGBColor color2 = RGBColor(3, 4, 1);
81 1 : RGBColor colorResult = RGBColor::interpolate(color1, color2, 0.5);
82 1 : RGBColor colorResult1 = RGBColor::interpolate(color1, color2, 0);
83 1 : RGBColor colorResult2 = RGBColor::interpolate(color1, color2, 1);
84 1 : EXPECT_TRUE(color1 == colorResult1);
85 1 : EXPECT_TRUE(color2 == colorResult2);
86 1 : EXPECT_EQ(2, colorResult.red());
87 1 : EXPECT_EQ(3, colorResult.green());
88 1 : EXPECT_EQ(2, colorResult.blue());
89 1 : }
90 :
91 : /* Test the method 'interpolate' with a weight of 0 and lower*/
92 1 : TEST(RGBColor, test_interpolate_weight_0) {
93 1 : RGBColor color1 = RGBColor(1, 2, 3);
94 1 : RGBColor color2 = RGBColor(2, 4, 2);
95 1 : RGBColor colorResult = RGBColor::interpolate(color1, color2, 0);
96 1 : RGBColor colorResult2 = RGBColor::interpolate(color1, color2, -1000);
97 1 : EXPECT_TRUE(colorResult == colorResult2);
98 1 : EXPECT_EQ(1, colorResult.red());
99 1 : EXPECT_EQ(2, colorResult.green());
100 1 : EXPECT_EQ(3, colorResult.blue());
101 1 : }
102 :
103 : /* Test the operator '=='. It has an tolerance of 1 */
104 1 : TEST(RGBColor, test_operator_equal) {
105 1 : RGBColor color1 = RGBColor(1, 2, 3);
106 1 : RGBColor color2 = RGBColor(1, 2, 3);
107 1 : EXPECT_TRUE(color1 == color2);
108 1 : color2 = RGBColor(1, 2, 4);
109 1 : EXPECT_FALSE(color1 == color2);
110 1 : }
|