Google Test only executing one test?
30 March, 2011 § 1 Comment
Earlier today while pair-programming, Angie and I ran into an issue where Google Test was only executing one unit test of the 150 or so in our test suite. We weren’t sure what the cause could be. We switched machines and all the unit tests ran fine on my machine.
We first checked the WinMain entry point to see if some configuration flag of GTest had been accidentally flipped. A quick diff showed us that nothing had changed.
So what could be differentiating Angie’s computer from mine? Well it turns out that Angie really enjoys using keyboard shortcuts and macros to help her do her work. A couple weeks ago I showed her some of the Visual Studio macros that are used in developing for Chromium. Angie saw the list of macros and quickly installed the “Alternate between source and header file”, as well as the “Run the currently selected google test”.
It turns out that the “Run the currently selected google test” works by adjusting the command line arguments that are passed to the GTest executable. This argument sets a filter on the test runner to only run the tests that match the given filter, which in this case happens to be the name of the currently selected test.
As the Jing states, simply clearing out the command argument fixed our problem. If you’re using the macro mentioned earlier, you can also place your cursor outside of a specific test case, run the macro, and the filter will be cleared.
I hope this post comes in handy for anyone else that got stuck in our position.
How to add a Makefile post-build step
24 September, 2010 § 1 Comment
I spend most of my time working in Visual Studio and like its ability for performing post-build steps. These can be used to copy files, register DLLs, or run unit tests.
In my work at MSU I need to use a makefile and have been writing my code test-first. As such, I found I was spending a lot of time typing make
and then typing ./runTests
. Ideally, I could just have the tests run automatically after each build.
Looking around on the internet for quick steps to add a post-build step to make wasn’t so fruitful. I ran in to a bunch of old forum posts that mentioned Visual Studio-generated makefiles with post-build steps. A little thinking about the idea sprang the solution straight to mind.
It turns out it is really simple and quite intuitive. Here’s my current makefile:
CC=g++ CFLAGS=-c -Wall -pedantic-errors -Wextra -Wunused -Werror all: findRoute findRoute-test runTests rebuild: clean all runTests: findRoute-test ./findRoute-test.exe findRoute: main.o cities.o flight_charges.o traffic_controller.o $(CC) main.o cities.o flight_charges.o traffic_controller.o -o findRoute findRoute-test: main-test.o cities.o flight_charges.o traffic_controller.o $(CC) main-test.o cities.o flight_charges.o traffic_controller.o -o findRoute-test main.o: main.cpp $(CC) $(CFLAGS) main.cpp cities.o: cities.cpp $(CC) $(CFLAGS) cities.cpp flight_charges.o: flight_charges.cpp $(CC) $(CFLAGS) flight_charges.cpp traffic_controller.o: traffic_controller.cpp $(CC) $(CFLAGS) traffic_controller.cpp main-test.o: main-test.cpp $(CC) $(CFLAGS) main-test.cpp clean: rm -rf *o findRoute*
The runTests
step above states that the findRoute-test
step is a prerequisite. If that step passes, then it simply executes the next line, that being the test executable.
I found this very intuitive as it is no different than any other executable like the compiler. I hope this helps someone else who is thinking of doing the same or a variation.
Do you see anything above that could be improved? I love getting comments that help me get better.
Initializing an array in C++
16 June, 2010 § 6 Comments
Most people know about member initialization lists in C++. Initialization lists allow you to initialize a variable when the class is instantiated, versus assigning a value within the constructor.
Have you ever tried to initialize an array? Many websites will tell you that this is not possible. In fact, if you try this you’ll get a compiler error:
class array { public: bool vars[2]; array() : vars[0](true), vars[1](true) {} };
BUT, if all you want to do is zero-initialize the array then you’re in luck! Simply place parens after the array and the array will be zero-initialized. Try out the code below:
#include <iostream> class arrayInit { public: bool vars[2]; arrayInit() : vars() {} }; class array { public: bool vars[2]; array() {} }; int main() { arrayInit a; std::cout << "a.vars[0] = " << a.vars[0] << std::endl; std::cout << "a.vars[1] = " << a.vars[1] << std::endl; array b; std::cout << "b.vars[0] = " << b.vars[0] << std::endl; std::cout << "b.vars[1] = " << b.vars[1] << std::endl; }
Using code like this in Visual Studio can generate warning C4351 because the behavior is different from previous versions of VC++. Here is the documentation from MSDN:
Compiler Warning (level 1) C4351
new behavior: elements of array ‘array’ will be default initialized
When an array is in a constructor’s member initialization list, the elements of the array will be default initialized. In previous versions of Visual C++, when an array was in a constructor’s member initialization list, the elements of the array may not have been default initialized in some cases.
If the array’s element type does not have a constructor, the elements of the array will be initialized with the corresponding zero representation for that type.
C4351 means that you should inspect your code. If you want the compiler’s previous behavior, remove the array from the constructor’s member initialization list.
If you want the new behavior, which is likely, because the array was explicitly added to the constructor’s member initialization list, use the warning pragma to disable the warning. The new behavior should be fine for most users.
One situation where the new behavior can result in unexpected behavior is when placement new is used to construct the object that has the array as a member, and the program depends on the contents that the memory (for the elements of the default initialized array) had before the call to placement new. In this case, the older compiler would have left the contents of memory unchanged, but the new behavior will cause default initialization of the array elements, overwriting the original contents in memory.
Digraphs and trigraphs in C++
15 June, 2010 § Leave a comment
This is a guest blog post by A.J. Orians.
In C++ you can program using digraphs and trigraphs. They are character substitutions you can make that may be easier to type depending on your keyboard layout. For instance instead of the following code:
int main() { int array[3]; return 0; }
Using digraphs and trigraphs it can be written as the following:
int main() ??< int array<:3:>; return 0; ??>
However Visual Studio will not compile using digraphs and trigraphs. But it is still important to know about them because you could be creating cross-platform code that may fail and it may not be easy to understand why. Take the following piece of code:
#include <vector> using namespace std; class A{}; int main() { vector<::A> vect; return 0; }
Under g++ it will fail to compile with the following error:
main.cpp: In function int main(): main.cpp:8: error: <:: cannot begin a template-argument list main.cpp:8: note: <: is an alternate spelling for [. Insert whitespace between < and :: main.cpp:8: note: (if you use -fpermissive G++ will accept your code)
The reason why is vect; which is an error. To fix this error just put a space between the < and the ::A.
Contributing to Open Source Software
27 May, 2010 § 4 Comments
Last week I attended the Google I/O conference. I went to some great sessions and learned a ton about Chrome, App Engine, and Android.
One of my biggest takeaways wasn’t from the sessions that I attended. I happened to bump in to some other college-aged guys and after some talking learned that they were contributors and committers to the Chromium project (the open source version of Google Chrome).
Now last year (2009) at the beginning of the summer, I wanted to participate in Google Summer of Code, specifically in one of the Google Chrome projects. I followed the steps on their developer setup guide, and kicked off a build on my machine. Hours later I realized that my machine was no match for Chromium.
Too much had to be installed to build the project and the file size of the project was leaps and bounds more than my pesky laptop could handle. I ended up putting away Chromium and focusing on tackling some books I had been wanting to read.
This summer is a whole new ball game. I’ve upgraded my computer and the Chromium project has also shed a lot of dependencies since May 2009. I’m happy to say that I was able to install Visual Studio 2008 Premium, SP1, a few hotfixes, and was off to building. An hour of waiting and I had my own build of Chromium running.
Today I can now say that I am a contributor to Chromium. I am proud that my name now resides on the AUTHORS file of Chromium, and I want to take this opportunity to pass-on some motivation for contributing to open source software.
If you’ve got an open source program that you use daily and get a lot out of, return the favor to the authors and help them out. Most open source software is free, so they’re not making money off of you using the software. It’s just kind of the fair thing to do.
Not to mention that there are tons of benefits that you will gain by contributing to an open source project. Let’s run through a list:
- Experience working with different code (paradigms, styles, patterns, algorithms)
- Get to work with developers located all across the world
- A great opportunity to learn more about a programming language
- A feeling of ownership of the product that you enjoy
- and so many more…
So take a second right now and think of the programs that you use and love. Are any of them open source? Do they have a list of bugs that could use some help? You could even take this opportunity to fix anything that you don’t like about the application and make a special build for yourself 🙂