Loki library tests and examples


This is the description of sample test programs for the Loki library. The library is described in the book "C++ Modern Design" by A. Alexandrescu (Addison Wesley 2001), and is available from sourceforge.

The programs are sorted by the chapters in the book. There is no sample program about chapter 1 (policies). AssocVector is described in Chapters 8 and 11, however i list the sample programs among those of Chapter 2 (Techniques).

First af all my tests are not aimed to be exhaustive, they are just sample applications of the Loki library. They have been compiled with gcc 3.2, and they seem to work properly. There is an annoying warning about using Test(...) with a non-POD type; this is used by Conversion, and is only checked syntactically, but never invoked. However the compiler does not get it.

Before you start, however you should download a patch for the Loki library. This patch does three things:

  1. it fixes a little glitch in AbstractFactory,
  2. it adds support for Field() to the LinearHierarchy,
  3. it adds MallocSPStorage, a storage policy based on malloc.

Also, here is the Makefile. Type make o compile all the tests. Alternately you can compile each test program individually with the command make program_name. The command make test runs all the tests and compare the results with the expected ones (these are generated with make oktest). Last, there are the cleanups, make clean and make distclean.

Finally here are few header files with sample class hierarchies used by some test programs,

Chapter 2. Techniques

Chapter 3. Typelists

Chapter 4. Small-Object Allocator

smallobj.cpp shows the usage of the small object allocator and the fixed allocator. Both expose the methods Allocate() and Deallocate(). The program also shows the usage of SmallObj template operators new() and delete().

Chapter 5. Generalized Functors

functor.cpp is a basic test of Functor. It uses function, class operator (), class member function, another functor, and copy assignment.

Chapter 6. Implementing Singletons

singleton.cpp shows the usage of the Singleton. I have tested only a few combinations of policies: creation was always with new, and threading was always single-threaded. Lifetime was default, phoenix, and longevity. I suppose that working with other combination of policies is just a matter of replacing the appropriate type in the definition.

Chapter 7. Smart Pointers

smart_ptr.cpp is a sample program using smart pointer. It has been checked with two checking policies (assert and reject-null) and two storage policies (default, and one based on malloc). smart_ptr_1.cpp is another program that checks that smart pointers destroy the memory when they go out of scope.

Chapter 8. Object Factories

Chapter 9. Abstract Factory

abstract_factory.cpp tests the abstract factory pattern with Loki AbstractFactory type. It tests also the concrete factory based on prototype.

Chapter 10. Visitor

Chapter 11. Multimethods

Additional software

There is a suite of extensive tests of Loki available on the web.


marco_corvi@oocities.com