F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sample1_unittest.cc
Go to the documentation of this file.
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.
31
32// This sample shows how to write a simple unit test for a function,
33// using Google C++ testing framework.
34//
35// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
36
37// Step 1. Include necessary header files such that the stuff your
38// test logic needs is declared.
39//
40// Don't forget gtest.h, which declares the testing framework.
41
42#include "sample1.h"
43
44#include <limits.h>
45
46#include "gtest/gtest.h"
47namespace {
48
49// Step 2. Use the TEST macro to define your tests.
50//
51// TEST has two parameters: the test case name and the test name.
52// After using the macro, you should define your test logic between a
53// pair of braces. You can use a bunch of macros to indicate the
54// success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
55// examples of such macros. For a complete list, see gtest.h.
56//
57// <TechnicalDetails>
58//
59// In Google Test, tests are grouped into test cases. This is how we
60// keep test code organized. You should put logically related tests
61// into the same test case.
62//
63// The test case name and the test name should both be valid C++
64// identifiers. And you should not use underscore (_) in the names.
65//
66// Google Test guarantees that each test you define is run exactly
67// once, but it makes no guarantee on the order the tests are
68// executed. Therefore, you should write your tests in such a way
69// that their results don't depend on their order.
70//
71// </TechnicalDetails>
72
73// Tests Factorial().
74
75// Tests factorial of negative numbers.
76TEST(FactorialTest, Negative) {
77 // This test is named "Negative", and belongs to the "FactorialTest"
78 // test case.
79 EXPECT_EQ(1, Factorial(-5));
80 EXPECT_EQ(1, Factorial(-1));
81 EXPECT_GT(Factorial(-10), 0);
82
83 // <TechnicalDetails>
84 //
85 // EXPECT_EQ(expected, actual) is the same as
86 //
87 // EXPECT_TRUE((expected) == (actual))
88 //
89 // except that it will print both the expected value and the actual
90 // value when the assertion fails. This is very helpful for
91 // debugging. Therefore in this case EXPECT_EQ is preferred.
92 //
93 // On the other hand, EXPECT_TRUE accepts any Boolean expression,
94 // and is thus more general.
95 //
96 // </TechnicalDetails>
97}
98
99// Tests factorial of 0.
100TEST(FactorialTest, Zero) { EXPECT_EQ(1, Factorial(0)); }
101
102// Tests factorial of positive numbers.
103TEST(FactorialTest, Positive) {
104 EXPECT_EQ(1, Factorial(1));
105 EXPECT_EQ(2, Factorial(2));
106 EXPECT_EQ(6, Factorial(3));
107 EXPECT_EQ(40320, Factorial(8));
108}
109
110// Tests IsPrime()
111
112// Tests negative input.
113TEST(IsPrimeTest, Negative) {
114 // This test belongs to the IsPrimeTest test case.
115
116 EXPECT_FALSE(IsPrime(-1));
117 EXPECT_FALSE(IsPrime(-2));
118 EXPECT_FALSE(IsPrime(INT_MIN));
119}
120
121// Tests some trivial cases.
122TEST(IsPrimeTest, Trivial) {
123 EXPECT_FALSE(IsPrime(0));
124 EXPECT_FALSE(IsPrime(1));
125 EXPECT_TRUE(IsPrime(2));
126 EXPECT_TRUE(IsPrime(3));
127}
128
129// Tests positive input.
130TEST(IsPrimeTest, Positive) {
131 EXPECT_FALSE(IsPrime(4));
132 EXPECT_TRUE(IsPrime(5));
133 EXPECT_FALSE(IsPrime(6));
134 EXPECT_TRUE(IsPrime(23));
135}
136} // namespace
137
138// Step 3. Call RUN_ALL_TESTS() in main().
139//
140// We do this by linking in src/gtest_main.cc file, which consists of
141// a main() function which calls RUN_ALL_TESTS() for us.
142//
143// This runs all the tests you've defined, prints the result, and
144// returns 0 if successful, or 1 otherwise.
145//
146// Did you notice that we didn't register the tests? The
147// RUN_ALL_TESTS() macro magically knows about all the tests we
148// defined. Isn't this convenient?
TEST(ComponentParameterTest, ParameterTest)
Definition Tests.cpp:91
int Factorial(int n)
Definition sample1.cc:35
bool IsPrime(int n)
Definition sample1.cc:45