]>
Commit | Line | Data |
---|---|---|
31f18b77 FG |
1 | // Copyright 2005, Google Inc. |
2 | // All rights reserved. | |
3 | // | |
4 | // Redistribution and use in source and binary forms, with or without | |
5 | // modification, are permitted provided that the following conditions are | |
6 | // met: | |
7 | // | |
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 | |
13 | // distribution. | |
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. | |
17 | // | |
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. | |
29 | ||
30 | // A sample program demonstrating using Google C++ testing framework. | |
31f18b77 FG |
31 | |
32 | // This sample shows how to write a more complex unit test for a class | |
33 | // that has multiple member functions. | |
34 | // | |
35 | // Usually, it's a good idea to have one test for each method in your | |
36 | // class. You don't have to do that exactly, but it helps to keep | |
37 | // your tests organized. You may also throw in additional tests as | |
38 | // needed. | |
39 | ||
40 | #include "sample2.h" | |
41 | #include "gtest/gtest.h" | |
1e59de90 | 42 | namespace { |
31f18b77 FG |
43 | // In this example, we test the MyString class (a simple string). |
44 | ||
45 | // Tests the default c'tor. | |
46 | TEST(MyString, DefaultConstructor) { | |
47 | const MyString s; | |
48 | ||
49 | // Asserts that s.c_string() returns NULL. | |
50 | // | |
51 | // <TechnicalDetails> | |
52 | // | |
53 | // If we write NULL instead of | |
54 | // | |
55 | // static_cast<const char *>(NULL) | |
56 | // | |
57 | // in this assertion, it will generate a warning on gcc 3.4. The | |
58 | // reason is that EXPECT_EQ needs to know the types of its | |
59 | // arguments in order to print them when it fails. Since NULL is | |
60 | // #defined as 0, the compiler will use the formatter function for | |
61 | // int to print it. However, gcc thinks that NULL should be used as | |
62 | // a pointer, not an int, and therefore complains. | |
63 | // | |
64 | // The root of the problem is C++'s lack of distinction between the | |
65 | // integer number 0 and the null pointer constant. Unfortunately, | |
66 | // we have to live with this fact. | |
67 | // | |
68 | // </TechnicalDetails> | |
1e59de90 | 69 | EXPECT_STREQ(nullptr, s.c_string()); |
31f18b77 FG |
70 | |
71 | EXPECT_EQ(0u, s.Length()); | |
72 | } | |
73 | ||
74 | const char kHelloString[] = "Hello, world!"; | |
75 | ||
76 | // Tests the c'tor that accepts a C string. | |
77 | TEST(MyString, ConstructorFromCString) { | |
78 | const MyString s(kHelloString); | |
79 | EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); | |
80 | EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1, | |
81 | s.Length()); | |
82 | } | |
83 | ||
84 | // Tests the copy c'tor. | |
85 | TEST(MyString, CopyConstructor) { | |
86 | const MyString s1(kHelloString); | |
87 | const MyString s2 = s1; | |
88 | EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString)); | |
89 | } | |
90 | ||
91 | // Tests the Set method. | |
92 | TEST(MyString, Set) { | |
93 | MyString s; | |
94 | ||
95 | s.Set(kHelloString); | |
96 | EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); | |
97 | ||
98 | // Set should work when the input pointer is the same as the one | |
99 | // already in the MyString object. | |
100 | s.Set(s.c_string()); | |
101 | EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); | |
102 | ||
103 | // Can we set the MyString to NULL? | |
1e59de90 TL |
104 | s.Set(nullptr); |
105 | EXPECT_STREQ(nullptr, s.c_string()); | |
31f18b77 | 106 | } |
1e59de90 | 107 | } // namespace |