googletest Hello World

Posted by Joe Yates Wed, 26 May 2010 13:17:00 GMT

This is a quick run down of how to get started with using googletest on Ubuntu.

Preparation

Assuming you have a working GCC build environment, all you have to do is install the googletest packages:

  $ sudo apt-get install libgtest0 libgtest-dev

Makefile

The only think of note about the Makefile is that it includes 'libgtest_main' - which implements main() and calls RUN_ALL_TESTS()

NAME = hello-world

LIBS = -lgtest_main

debug: all
run-debug:
	./${NAME}

all: $(NAME).o
	c++ -lstdc++ $(LIBS) -o $(NAME) $(NAME).o

compile: $(NAME).o

clean:
	find . -name '*.o' -exec rm -f {} ';'
	find . -name $(NAME) -exec rm -f {} ';'

$(NAME).o: $(NAME).c++
	gcc -c -I. -o $(NAME).o $(NAME).c++

.c++.o:
	gcc -c -I. -o [email protected] $<

Source

I've put everything into a single source file 'hello-world.c++' to keep things minimal:

/////////////////////////////
// In the header file

#include <sstream>
using namespace std;

class Salutation
{
public:
  static string greet(const string& name);
};

///////////////////////////////////////
// In the class implementation file

string Salutation::greet(const string& name) {
  ostringstream s;
  s << "Hello " << name << "!";
  return s.str();
}

///////////////////////////////////////////
// In the test file
#include <gtest/gtest.h>

TEST(SalutationTest, Static) {
  EXPECT_EQ(string("Hello World!"), Salutation::greet("World"));
}

Compilation

Just run:

  $ make

Output

This test produces the following:

$ ./hello-world
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SalutationTest
[ RUN      ] SalutationTest.Static
[       OK ] SalutationTest.Static
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran.
[  PASSED  ] 1 test.

Conclusion

It couldn't really be much simpler!

Object Representation in C++

Posted by Joe Yates Wed, 12 May 2010 13:42:00 GMT

In this post I investigate the way that C++ objects' data and functions are laid out in memory by the two best known compilers: GCC and Microsoft's Visual C++.

A Rails Programmer's First Django Application 1

Posted by Joe Yates Sun, 04 Apr 2010 15:35:00 GMT

I wrote my first mini Django application - a local history timeline - to get an idea of the differences between Django and Rails.

Editing Javascript in Emacs

Posted by Joe Yates Fri, 02 Apr 2010 10:06:00 GMT

Steve Yegge of Google has open-sourced his Javascript mode for Emacs. The main features are: true program parsing, code highlighting and error highlighting.

A Sunrise and Sunset Time Calculator

Posted by Joe Yates Tue, 09 Mar 2010 09:14:00 GMT

I've created a new Ruby library that calculates sunrise and sunset times.

i18n Internationalization without Rails

Posted by Joe Yates Thu, 04 Mar 2010 23:30:00 GMT

I was unable to find examples of using the Ruby internationalization gem (i18n) outside of Rails apps. In this post I give a minimal "Ciao Mondo!" example.

Handling Multiple Versions of Ruby

Posted by Joe Yates Mon, 22 Feb 2010 23:04:00 GMT

Use Debian Alternatives to switch between Ruby versions (1.8, 1.9 and JRuby).

JRuby on Rails

Posted by Joe Yates Mon, 22 Feb 2010 12:06:00 GMT

How to set up JRuby and adapt a Ruby on Rails application to run on top of it.

Trying out Clojure

Posted by Joe Yates Tue, 09 Feb 2010 13:58:00 GMT

Clojure is a very promising new language that runs on the JVM. It handles concurrency in effective end innovative ways. I've written a mini-guide on how to get started.

Regular Expressions in SQLite with Ruby

Posted by Joe Yates Sun, 31 Jan 2010 11:30:00 GMT

How to implement the SQLite's 'REGEX' operator in Ruby on Rails.