1aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Copyright 2009, Google Inc.
2aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// All rights reserved.
3aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
4aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Redistribution and use in source and binary forms, with or without
5aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// modification, are permitted provided that the following conditions are
6aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// met:
7aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
8aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Redistributions of source code must retain the above copyright
9aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// notice, this list of conditions and the following disclaimer.
10aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Redistributions in binary form must reproduce the above
11aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// copyright notice, this list of conditions and the following disclaimer
12aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// in the documentation and/or other materials provided with the
13aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// distribution.
14aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//     * Neither the name of Google Inc. nor the names of its
15aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// contributors may be used to endorse or promote products derived from
16aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// this software without specific prior written permission.
17aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
18aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
30aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Author: wan@google.com (Zhanyong Wan)
31aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
32aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// Tests Google Test's throw-on-failure mode with exceptions disabled.
33aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter//
34aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// This program must be compiled with exceptions disabled.  It will be
35aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// invoked by gtest_throw_on_failure_test.py, and is expected to exit
36aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// with non-zero in the throw-on-failure mode or 0 otherwise.
37aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
38aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include "gtest/gtest.h"
39aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
40aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include <stdio.h>                      // for fflush, fprintf, NULL, etc.
41aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include <stdlib.h>                     // for exit
42aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#include <exception>                    // for set_terminate
43aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
44aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// This terminate handler aborts the program using exit() rather than abort().
45aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// This avoids showing pop-ups on Windows systems and core dumps on Unix-like
46aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter// ones.
47aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchtervoid TerminateHandler() {
48aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
49aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  fflush(NULL);
50aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  exit(1);
51aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
52aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
53aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchterint main(int argc, char** argv) {
54aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#if GTEST_HAS_EXCEPTIONS
55aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  std::set_terminate(&TerminateHandler);
56aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter#endif
57aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  testing::InitGoogleTest(&argc, argv);
58aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
59aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // We want to ensure that people can use Google Test assertions in
60aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // other testing frameworks, as long as they initialize Google Test
61aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // properly and set the throw-on-failure mode.  Therefore, we don't
62aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // use Google Test's constructs for defining and running tests
63aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // (e.g. TEST and RUN_ALL_TESTS) here.
64aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
65aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // In the throw-on-failure mode with exceptions disabled, this
66aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // assertion will cause the program to exit with a non-zero code.
67aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  EXPECT_EQ(2, 3);
68aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter
69aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // When not in the throw-on-failure mode, the control will reach
70aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  // here.
71aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter  return 0;
72aa46da279e2426caf1e103eb079dfec8124c5feeCourtney Goeltzenleuchter}
73