Blame googletest/samples/sample2_unittest.cc

Packit bd1cd8
// Copyright 2005, Google Inc.
Packit bd1cd8
// All rights reserved.
Packit bd1cd8
//
Packit bd1cd8
// Redistribution and use in source and binary forms, with or without
Packit bd1cd8
// modification, are permitted provided that the following conditions are
Packit bd1cd8
// met:
Packit bd1cd8
//
Packit bd1cd8
//     * Redistributions of source code must retain the above copyright
Packit bd1cd8
// notice, this list of conditions and the following disclaimer.
Packit bd1cd8
//     * Redistributions in binary form must reproduce the above
Packit bd1cd8
// copyright notice, this list of conditions and the following disclaimer
Packit bd1cd8
// in the documentation and/or other materials provided with the
Packit bd1cd8
// distribution.
Packit bd1cd8
//     * Neither the name of Google Inc. nor the names of its
Packit bd1cd8
// contributors may be used to endorse or promote products derived from
Packit bd1cd8
// this software without specific prior written permission.
Packit bd1cd8
//
Packit bd1cd8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit bd1cd8
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit bd1cd8
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit bd1cd8
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit bd1cd8
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit bd1cd8
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit bd1cd8
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit bd1cd8
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit bd1cd8
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit bd1cd8
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit bd1cd8
Packit bd1cd8
// A sample program demonstrating using Google C++ testing framework.
Packit bd1cd8
//
Packit bd1cd8
// Author: wan@google.com (Zhanyong Wan)
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
// This sample shows how to write a more complex unit test for a class
Packit bd1cd8
// that has multiple member functions.
Packit bd1cd8
//
Packit bd1cd8
// Usually, it's a good idea to have one test for each method in your
Packit bd1cd8
// class.  You don't have to do that exactly, but it helps to keep
Packit bd1cd8
// your tests organized.  You may also throw in additional tests as
Packit bd1cd8
// needed.
Packit bd1cd8
Packit bd1cd8
#include "sample2.h"
Packit bd1cd8
#include "gtest/gtest.h"
Packit bd1cd8
Packit bd1cd8
// In this example, we test the MyString class (a simple string).
Packit bd1cd8
Packit bd1cd8
// Tests the default c'tor.
Packit bd1cd8
TEST(MyString, DefaultConstructor) {
Packit bd1cd8
  const MyString s;
Packit bd1cd8
Packit bd1cd8
  // Asserts that s.c_string() returns NULL.
Packit bd1cd8
  //
Packit bd1cd8
  // <TechnicalDetails>
Packit bd1cd8
  //
Packit bd1cd8
  // If we write NULL instead of
Packit bd1cd8
  //
Packit bd1cd8
  //   static_cast<const char *>(NULL)
Packit bd1cd8
  //
Packit bd1cd8
  // in this assertion, it will generate a warning on gcc 3.4.  The
Packit bd1cd8
  // reason is that EXPECT_EQ needs to know the types of its
Packit bd1cd8
  // arguments in order to print them when it fails.  Since NULL is
Packit bd1cd8
  // #defined as 0, the compiler will use the formatter function for
Packit bd1cd8
  // int to print it.  However, gcc thinks that NULL should be used as
Packit bd1cd8
  // a pointer, not an int, and therefore complains.
Packit bd1cd8
  //
Packit bd1cd8
  // The root of the problem is C++'s lack of distinction between the
Packit bd1cd8
  // integer number 0 and the null pointer constant.  Unfortunately,
Packit bd1cd8
  // we have to live with this fact.
Packit bd1cd8
  //
Packit bd1cd8
  // </TechnicalDetails>
Packit bd1cd8
  EXPECT_STREQ(NULL, s.c_string());
Packit bd1cd8
Packit bd1cd8
  EXPECT_EQ(0u, s.Length());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
const char kHelloString[] = "Hello, world!";
Packit bd1cd8
Packit bd1cd8
// Tests the c'tor that accepts a C string.
Packit bd1cd8
TEST(MyString, ConstructorFromCString) {
Packit bd1cd8
  const MyString s(kHelloString);
Packit bd1cd8
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
Packit bd1cd8
  EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
Packit bd1cd8
            s.Length());
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests the copy c'tor.
Packit bd1cd8
TEST(MyString, CopyConstructor) {
Packit bd1cd8
  const MyString s1(kHelloString);
Packit bd1cd8
  const MyString s2 = s1;
Packit bd1cd8
  EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
Packit bd1cd8
}
Packit bd1cd8
Packit bd1cd8
// Tests the Set method.
Packit bd1cd8
TEST(MyString, Set) {
Packit bd1cd8
  MyString s;
Packit bd1cd8
Packit bd1cd8
  s.Set(kHelloString);
Packit bd1cd8
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
Packit bd1cd8
Packit bd1cd8
  // Set should work when the input pointer is the same as the one
Packit bd1cd8
  // already in the MyString object.
Packit bd1cd8
  s.Set(s.c_string());
Packit bd1cd8
  EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
Packit bd1cd8
Packit bd1cd8
  // Can we set the MyString to NULL?
Packit bd1cd8
  s.Set(NULL);
Packit bd1cd8
  EXPECT_STREQ(NULL, s.c_string());
Packit bd1cd8
}