Simple unit testing in C++

There are many complex frameworks available, include cxxtest and the Boost test frameworks. However, a simple approach can also be effective.

In our simple approach, we define a static public method for each class: static void unit_test(). This method runs all the unit tests associated with the class. It throws if it finds an error; otherwise it returns.

As the start of a main program that uses classes, I run a series of tests on each class. The tests look like

In writing the unit_test() methods, there is some repetitive code. For example, I do a lot of scientific computing, and its useful to test if two floating point values a and bare close enough. For this, I defined a function bool close(T a, T b, T tolerance). All these function are static methods in the class Unit_test.

Posted in C++ | Leave a comment

gnu screen how to’s

The GNU screen manual

Setup the ~/.screenrc file such that many lines of stdout are saved, because by default, screen throws away the output. Put these lines in .screenrc
defscrollback 10000

ssh to remote system

Either start a new screen session or reconnect to a prior session
screen #start new session and attach to it
screen -list #list all available session and whether attached or not
screen -dr [pid.sessionname] # connect attached session to me
screen -r [pid.sessionname] # connect detached session to me

Once you are attached to a session, never use the session command by itself, or you start yet another session. It’s easy to get confused.

Once attached to a session, you can open windows (= virtual terminals).

To create a new window and switch to it and start recording in a file everything that displays on the window:
C-a c  # create window and switch to it
C-a ” # list current window numbers for use in script command below
script window-N-MM-DD-HH-MM-PURPOSE.script #save stdout in unique file name

With a screen session, to switch to a different window
C-a N # switch to window N
C-a p  # switch to previous window
C-a n  # switch to next window

To scroll a window, switch to it and enter copy mode (which allows scrolling using the arrow keys)
C-a ESC #start copy mode
<scroll with arrow keys, if running Terminal, can copy to clipboard>
ESC #stop copy mode

NOTE: DO NOT create another screen session from within a screen session. If you do, then follow Marco’s advice

Problem with screen w/in a screen is that there is no way to send
the C-a to the nested screen.  The best way to sort out the problem
is to connect to the nested screen directly using

screen -rd <id of screen>

the ‘d’ parameter will detach the nested screen then in the other
‘nesting’ screen you will have the terminal back.

Posted in linux/unix | Leave a comment

Using the servers

Server names (partial list): juno, monk

To see load on servers, while on campus  browse to

From home, must login twice and its best to always run screen
screen [-r]

My ubunutu vm (or the mac os logon) has a script to sync the generated-data
rsync -aP repp-generated-data

Posted in linux/unix | Leave a comment

Git remote repositories

To clone a remote repository
git clone ssh:// repo.git

To retrieve changes from the remote and merge into a local branch

To send changes in current branch to the remove
git push

Posted in git | Leave a comment

Branching in git

To create a new branch from the current branch
git branch BRANCH_NAME

to list all branches
git branch

To work in a branch
git checkout BRANCH_NAME

To merge commits in BRANCH_NAME into the master branch
git checkout master
git merge BRANCH_NAME

To delete a branch
git branch -d BRANCH_NAME

To rename a branch

Posted in git | Leave a comment

Using git as a front-end for Subversion


Checking out a subversion repository to a git repository in your LOCAL_DIR

Edit and commit with git as normal. In particular, you can create a new git branch with
and later merge it back to the master with
git checkout master

To update and commit back to Subversion
git-svn rebase
git-svn dcommit

Posted in scm | Leave a comment

Skeleton C++ type polymorphism (virtual function)

Here is one recipe; there are others.

Plan to access the polymorphic function via a pointer, as in

Define the base class this way
struct Base {
virtual int func() { … };

Define the derived class this way
struct Derived : public Base {
virtual int func() { … };

Define objects this way
Base base_obj;
Derived derived_obj;
std::vector<Base*> mixed;

Call the virtual function this way
function do_it(std::vector<Base*> v) {
for (unsigned i = 0; i < v.size(); ++i) {>func();

Posted in C++ | Leave a comment