Tag Archives: programming

Setting travis-ci with github for a c++ project for the first time

Intro

Here, I’m trying to use travis-ci, c++, github, CATCH, premake together with my undoredo-cpp library to reduce entropy, try out continuous integration and behavior-style tests.

As a “one-man show” programmer at least at home, I’m trying to keep the discipline of writing tests first. “Growing Object-Oriented Software Guided by Tests” is perhaps a good, although a comparatively dry book for those who are not yet convinced. The blog post by Phil Nash about his latest version of the c++ single-header testing framework CATCH, moved me to finally get my hands on the free continuous integration service travis-ci, along with CATCH with a goal to rewrite the tests for my undo-redo c++ adventure in a more behavior-driven-style.

The undo-redo library is already there, and the tests as well – in gtest (see the master branch). I’d label them “explorative” at the moment since there are just too many assertions per test case, which means I’m repeating myself.

Starting continuous integration at travis-ci for c++

To start my CATCH-“BDD” exploration I’ve setup the branch first: https://github.com/d-led/undoredo-cpp/tree/catchmoci. At my landing page the project is switched on for catching the commit hooks:

travis_setup

The following configuration file .travis.yml is placed for travis-ci to know what to do with my non-conforming repo:

language: cpp

branches:
  only:
   - catchmoci

before_script: ls

script:
  - make -C Build

As in all TDD practice, the build fails due to the reason that the build doesn’t work yet at all. Adding a status image to my README shines:

failing

Fixing the build

The makefiles are created using premake4, which is a single-file makefile generator based on lua. Unfortunately, I couldn’t force the CI-virtual machine execute my binary premake4, so I had to add the generated makefiles. Now that the make process works, the tests still don’t compile:

failing

Fixing the tests

Once the bulk of the assertions have been rewritten for CATCH, the build still failed due to an ambiguity in serializing std::nullptr_t. Fortunately, Phil has thought of (or rather tested) that, and has a macro which can be defined for the build, fixing it: CATCH_CONFIG_CPP11_NULLPTR.

Voila, travis-ci vm is happy:

passing

Just checking locally if test reporting is fine by adding a spurious test temporarily:

justchecking

The test-rewrite has been successful and all pass, the badge is green and I can go to bed

passing2

But! It’s not the end of the story! BDD! Mock-objects!

Self-destructing objects

One day, recalling the programmed cell death I decided to create class that would self-destruct on a certain amount of copying.

Today’s little research resulted in the following project: https://github.com/d-led/selfdestructing

The question arises, what exactly copying stands for. I could think of four policies. The implementation of those policies is configurable and extensible. Right now there is no explicitly thread-safe policy implemented, but those would not be very complicated, if the purpose is debugging solely.

Usage

#include "crash_on_copy.hpp"
struct TestNumberCrash : public crashes::on<3>::copies {};

on_copiesCrashes on 3 total copies of the originally created object, but doesn’t crash on 3 total instances of the class

struct TestCopyNrCrash : public crashes::after<3>::copies {};

after_copiesCrashes on any third copy of the original object, but doesn’t crash on 3 total instances of the class

struct TestTotalNrCrash : public crashes::on_total<3,TestTotalNrCrash>::instances {};

on_total_instancesCrashes on instantiation of an object if 2 objects are alive, but doesn’t crash on any creation if the total amount of instances is below 2

struct TestAfterTotalNrCrash : public crashes::after_total<3,TestAfterTotalNrCrash>::instances {};

after_total_instancesCrashes after a third object instantiation of the class

Singular form aliases are also available, i.e. crashes::on<1>::copy.

The project at github contains the test showing the crashing pattern.

a generic c++ lua stack crawler

Sometimes one needs to walk the Lua stack on the C++ side, for example, if passing tables to C++ via the new extended LuaBridge functionality.
For that I’ve started luastackcrawler (see on github). The small test shows that it works at least in principle.
A possible use case – configuration. If the layout of a stack entry, such as a table is known, one can write own wrappers around those to easily access the values. Right now one can iterate through stack and Lua table values, which can be of several types, represented by a boost::variant.

Example, lua:

and the c++ implementation of ArraySize:

int ArraySize(boost::shared_ptr<LuaTable> T)
{
    if (!T)
        return 0;

    return std::count_if(
        T->begin(),
        T->end(),
        [](std::pair<LuaMultiValue,LuaMultiValue> const& entry) {
            return GetType(entry.first) == LuaType::NUMBER;
        });
}

lua + pugixml via LuaBridge

Started working on a binding of pugixml for lua: https://github.com/d-led/pugilua

pugixml supports utf-8, so, using icu4lua one could, in principle, have a nice lua-style processing of XML files. The interface is right now kept almost one-to-one to the original. It shows, at least for me, how easy it is to make bindings to C++ using LuaBridge and how easy the usage of pugixml is.

C++ humor: a –do not modify the comments or format– horror code

just in case you think, you can understand what the code is doing by looking at it

executable at Ideone

#include <iostream>
#define B(l,a) __##l##E##__
#define _O_ ((1<<2>>1<<1)-2)
#define _h_ 7-3
#define _C_ (7/_h_)
#define _r_ ((1<<0)+0x00a/2+_C_)
#define _a_ ((1<<5)-_r_)
#define _p_ { void _(){} namespace
#define printf(_0_) squeak(_O_,B(LIN,!)-_a_); _
 
// this
// is weird
 
namespace _p_ {
void squeak(int,int who) {      switch (who) {
                case _O_: std::cout<<"\x71\x75\x61\x6b"<<std::endl; break;
                case (_h_+_C_-2): std::cout<<"\x6d\x65\x6f\x77"<<std::endl; break;
                case _h_: std::cout<<"\x62\x6f\x77"<<std::endl; break;
                default: std::cout<<"\x62\x6c\x61\x68"<<std::endl; break;
        };
}}
}
 
// do not review ...
 
int main()
{
        printf("hello\n");
        printf("this\n");
        printf("is\n");
        printf("not\n");
        printf("funny\n");
        printf("at all!\n");
        return 0;
}
 
//expected output:
//meow
//blah
//quak
//blah
//bow
//blah

 

On unwanted access to inner classes in C++11

#include <iostream>
#include <typeinfo>

class Outer {
private:
	class Inner {
	public:
		void Do() {
			std::cout<<"Do"<<std::endl;
		}
	};
private:
	Inner inner;
public:
	Inner& Get() {
		return inner;
	}
};

int main()
{
	Outer O;
	// err: private Outer::Inner
    // Outer::Inner& x=O.Get(); x.Do();
    auto& x=O.Get();
    x.Do(); // ok
    std::cout << typeid(x).name() << std::endl;  
    O.Get().Do(); // ok
    return 0;
}