1
// Copyright 2005, Google Inc.
2
// 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.
30
// A sample program demonstrating using Google C++ testing framework.
32
// Author: wan@google.com (Zhanyong Wan)
35
// This sample shows how to write a more complex unit test for a class
36
// that has multiple member functions.
38
// Usually, it's a good idea to have one test for each method in your
39
// class. You don't have to do that exactly, but it helps to keep
40
// your tests organized. You may also throw in additional tests as
44
#include <gtest/gtest.h>
46
// In this example, we test the MyString class (a simple string).
48
// Tests the default c'tor.
49
TEST(MyString, DefaultConstructor) {
52
// Asserts that s.c_string() returns NULL.
56
// If we write NULL instead of
58
// static_cast<const char *>(NULL)
60
// in this assertion, it will generate a warning on gcc 3.4. The
61
// reason is that EXPECT_EQ needs to know the types of its
62
// arguments in order to print them when it fails. Since NULL is
63
// #defined as 0, the compiler will use the formatter function for
64
// int to print it. However, gcc thinks that NULL should be used as
65
// a pointer, not an int, and therefore complains.
67
// The root of the problem is C++'s lack of distinction between the
68
// integer number 0 and the null pointer constant. Unfortunately,
69
// we have to live with this fact.
71
// </TechnicalDetails>
72
EXPECT_STREQ(NULL, s.c_string());
74
EXPECT_EQ(0, s.Length());
77
const char kHelloString[] = "Hello, world!";
79
// Tests the c'tor that accepts a C string.
80
TEST(MyString, ConstructorFromCString) {
81
const MyString s(kHelloString);
82
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
83
EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
87
// Tests the copy c'tor.
88
TEST(MyString, CopyConstructor) {
89
const MyString s1(kHelloString);
90
const MyString s2 = s1;
91
EXPECT_TRUE(strcmp(s2.c_string(), kHelloString) == 0);
94
// Tests the Set method.
99
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
101
// Set should work when the input pointer is the same as the one
102
// already in the MyString object.
104
EXPECT_TRUE(strcmp(s.c_string(), kHelloString) == 0);
106
// Can we set the MyString to NULL?
108
EXPECT_STREQ(NULL, s.c_string());