From adeef73a97cf191301a7b563883a075caed733b7 Mon Sep 17 00:00:00 2001 From: Damien Martin-Guillerez Date: Tue, 24 Mar 2015 09:05:08 +0000 Subject: Move all examples into Bazel workspace Those examples will be used for Bazel integration tests and their sources should be available directly to the Bazel workspace itself. -- MOS_MIGRATED_REVID=89380736 --- examples/cpp/BUILD | 25 +++++++++++++++++++++++++ examples/cpp/README.md | 4 ++++ examples/cpp/hello-fail.cc | 22 ++++++++++++++++++++++ examples/cpp/hello-lib.cc | 21 +++++++++++++++++++++ examples/cpp/hello-lib.h | 23 +++++++++++++++++++++++ examples/cpp/hello-world.cc | 26 ++++++++++++++++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 examples/cpp/BUILD create mode 100644 examples/cpp/README.md create mode 100644 examples/cpp/hello-fail.cc create mode 100644 examples/cpp/hello-lib.cc create mode 100644 examples/cpp/hello-lib.h create mode 100644 examples/cpp/hello-world.cc (limited to 'examples/cpp') diff --git a/examples/cpp/BUILD b/examples/cpp/BUILD new file mode 100644 index 0000000000..195bbb3d25 --- /dev/null +++ b/examples/cpp/BUILD @@ -0,0 +1,25 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "hello-lib", + srcs = ["hello-lib.cc"], + hdrs = ["hello-lib.h"], +) + +cc_binary( + name = "hello-world", + srcs = ["hello-world.cc"], + deps = [":hello-lib"], +) + +cc_test( + name = "hello-success_test", + srcs = ["hello-world.cc"], + deps = [":hello-lib"], +) + +cc_test( + name = "hello-fail_test", + srcs = ["hello-fail.cc"], + deps = [":hello-lib"], +) diff --git a/examples/cpp/README.md b/examples/cpp/README.md new file mode 100644 index 0000000000..fba714321c --- /dev/null +++ b/examples/cpp/README.md @@ -0,0 +1,4 @@ +C++ Examples +============ + +These examples demonstrate building C++ binaries, libraries, and tests. diff --git a/examples/cpp/hello-fail.cc b/examples/cpp/hello-fail.cc new file mode 100644 index 0000000000..5b561cb0ce --- /dev/null +++ b/examples/cpp/hello-fail.cc @@ -0,0 +1,22 @@ +#include "examples/cpp/hello-lib.h" + +#include + +using hello::HelloLib; +using std::string; + +/** + * This is a fake test that prints "Hello barf" and then exits with exit code 1. + * If run as a test (cc_test), the non-0 exit code indicates to Bazel that the + * "test" has failed. + */ +int main(int argc, char** argv) { + HelloLib lib; + string thing = "barf"; + if (argc > 1) { + thing = argv[1]; + } + + lib.greet(thing); + return 1; +} diff --git a/examples/cpp/hello-lib.cc b/examples/cpp/hello-lib.cc new file mode 100644 index 0000000000..8e40921cee --- /dev/null +++ b/examples/cpp/hello-lib.cc @@ -0,0 +1,21 @@ +#include "examples/cpp/hello-lib.h" + +#include + +using std::cout; +using std::endl; +using std::string; + +namespace hello { + +HelloLib::HelloLib() : HelloLib("Hello") { +} + +HelloLib::HelloLib(const string& greeting) : greeting_(new string(greeting)) { +} + +void HelloLib::greet(const string& thing) { + cout << *greeting_ << " " << thing << endl; +} + +} // namespace hello diff --git a/examples/cpp/hello-lib.h b/examples/cpp/hello-lib.h new file mode 100644 index 0000000000..555d177936 --- /dev/null +++ b/examples/cpp/hello-lib.h @@ -0,0 +1,23 @@ +#ifndef EXAMPLES_CPP_HELLO_LIB_H_ +#define EXAMPLES_CPP_HELLO_LIB_H_ + +#include +#include + +namespace hello { + +class HelloLib { + public: + HelloLib(); + + explicit HelloLib(const std::string &greeting); + + void greet(const std::string &thing); + + private: + std::unique_ptr greeting_; +}; + +} // namespace hello + +#endif // EXAMPLES_CPP_HELLO_LIB_H_ diff --git a/examples/cpp/hello-world.cc b/examples/cpp/hello-world.cc new file mode 100644 index 0000000000..be435c3256 --- /dev/null +++ b/examples/cpp/hello-world.cc @@ -0,0 +1,26 @@ +#include "examples/cpp/hello-lib.h" + +#include + +using hello::HelloLib; +using std::string; + +/** + * This prints "Hello world". If it is run with arguments, it will use the first + * argument instead of "world". Build and run //examples/cpp:hello-world to see + * this program in action. + * + * This file does double-duty as a "test." It is a cc_binary, so it can also be + * compiled as a cc_test and Bazel will decide on whether it passed or failed + * based on exit code (which is always 0 here, so the test "passes"). See + * hello-fail.cc for an example of making a test fail. + */ +int main(int argc, char** argv) { + HelloLib lib("Hello"); + string thing = "world"; + if (argc > 1) { + thing = argv[1]; + } + lib.greet(thing); + return 0; +} -- cgit v1.2.3