Over-large Code Segments Code

From Open Watcom

Jump to: navigation, search

Contents

Phase 1

This is the initial test framework.

The C Files

Three files are part of the initial test framework:

  1. test.h
  2. Cmain.c
  3. Ctest1.c

File: test.h

// Declares the test functions invoked by main().

void test1( void );

File: Cmain.c

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.h"

int main( void )
{
    test1();

    return( EXIT_SUCCESS );
}

File: Ctest1.c

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test1( void )
{
    int Fred = 0;
    int Judy = 0;

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

(999 more repetitions)

    return;
}

The comments indicate how the file is created: first do ten repetitions, then put the 10 reps comment, then do ten repetitions of that entire section, then put the 100 reps comment, and do ten repetitions of that entire section, and finally put the 1000 reps comment. This process is used whenever this sort of function is created.

The C++ Files

Three files are part of the initial test framework:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp

File: test.hpp

// This is the header for the test class.

class testClass1
{
    private:

    int Fred;
    int Judy;

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    testClass1 & operator=( const testClass1 & ) { return *this; }
    void test1();
};

File: CPPmain.cpp

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    tester1.test1();

    return( EXIT_SUCCESS );
}

File: CPPtest1.cpp

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{

// 5000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);
(4999 more repetitions)

    return;
}

The function was created by copying the one from test1.c and renaming it. The number of reps was increased to 5000 because versions containing 1000, 2000, 3000 and 4000 reps were compiled and linked.

Phase 2

When the external refactorization procedure outlined in the tutorial has been applied, then the test framework will look like this:

The C Files

After external refactoring, the test framework has four files:

  1. test.h
  2. Cmain.c
  3. Ctest1.c
  4. Ctest2.c

File test.h:

// Declares the test functions invoked by main().

void test1( void );
void test2( void );

File Cmain.c:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.h"

int main( void )
{
    test1();
    test2();

    return( EXIT_SUCCESS );
}

File Ctest1.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test1( void )
{
    int Fred = 0;
    int Judy = 0;

// 500 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <499 additional repetitions>

    return;
}

File Ctest2.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test2( void )
{
    int Fred = 0;
    int Judy = 0;

// 500 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <499 additional repetitions>

    return;
}

The C++ Files

After external refactoring, the test framework has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

class testClass1
{
    private:
    
    int Fred;
    int Judy;

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    testClass1 & operator=( const testClass1 & ) { return *this; }
    void test1();
    void test2();
};

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
  testClass1 tester1;
  tester1.test1();
  tester1.test2();

  return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{

// 2500 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <2499 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2500 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <2499 additional repetitions>

    return;
}

Phase 3

This shows the test framework after the number of repetitions has been reduced:

The C Files

The test framework still has four files:

  1. test.h
  2. Cmain.c
  3. Ctest1.c
  4. Ctest2.c

File test.h:

// Declares the test functions invoked by main().

void test1( void );
void test2( void );

File Cmain.c:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.h"

int main( void )
{
    test1();
    test2();

    return( EXIT_SUCCESS );
}

File Ctest1.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test1( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

File Ctest2.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test2( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

class testClass1
{
    private:
    
    int Fred;
    int Judy;

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    testClass1 & operator=( const testClass1 & ) { return *this; }
    void test1();
    void test2();
};

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
  testClass1 tester1;
  tester1.test1();
  tester1.test2();

  return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

Phase 4

When the internal refactorization procedure outlined in the tutorial has been applied, then the test framework will look like this:

The C Files

The test framework still has four files:

  1. test.h
  2. Cmain.c
  3. Ctest1.c
  4. Ctest2.c

File test.h:

// Declares the test functions invoked by main().

void test1( void );
void test2( void );

File Cmain.c:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.h"

int main( void )
{
    test1();
    test2();

    return( EXIT_SUCCESS );
}

File Ctest1.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

static void test11( void );
static void test12( void );

void test1( void )
{
    test11();
    test12();
}

void test11( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

void test12( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

File Ctest2.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test2( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

     <399 additional repetitions>

    return;
}

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

class testClass1
{
    private:

    int Fred;
    int Judy;

    void test11();
    void test12();

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    testClass1 & operator=( const testClass1 & ) { return *this; }
    void test1();
    void test2();
};

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    tester1.test1();
    tester1.test2();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{
    test11();
    test12();
}

void testClass1::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

void testClass1::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

Phase 5

This is the test framework after being adapted to use inline functions.

The C Files

The test framework still has four files:

  1. test.h
  2. Cmain.c
  3. Ctest1.c
  4. Ctest2.c

File test.h:

// Declares the test functions.

#include <stdio.h>

void test1( void );
void test12( void );
void test13( void );
void test2( void );

inline void test12( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

inline void test13( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

File Cmain.c:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.h"

int main( void )
{
    test1();
    test2();
    test12();
    test13();

    return( EXIT_SUCCESS );
}

File Ctest1.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

static void test11( void );

void test1( void )
{
    test11();
    test13();
}

void test11( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    return;
}

File Ctest2.c:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.h"

void test2( void )
{
    int Fred = 0;
    int Judy = 0;

// 400 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <399 additional repetitions>

    test13();

    return;
}

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class testClass1
{
    private:

    int Fred;
    int Judy;

    void test11();

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    testClass1 & operator=( const testClass1 & ) { return *this; }
    void test1();
    void test2();
    void test12();
    void test13();
};

inline void testClass1::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

inline void testClass1::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{
    test11();
    test13();
}

void testClass1::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    test13();

    return;
}

Phase 6

This is the test framework with a slight modification to show how virtual inline functions are treated by the compiler.

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class testClass1
{
    private:

            int Fred;
            int Judy;

            void test11();

    public:

            testClass1() : Fred(0), Judy(0) {}
            testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
           ~testClass1() {}

            testClass1 & operator=( const testClass1 & ) { return *this; }
            void test1();
            void test2();
    virtual void test12();
    virtual void test13();
};

inline void testClass1::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

inline void testClass1::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{
    testClass1 tester;

    tester.test11();
    tester.test13();
}

void testClass1::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    test13();

    return;
}

Phase 7

This is the test framework slightly modified to show how to get main() to compile while invoking both test12() and test13().

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class testClass1
{
    private:

            int Fred;
            int Judy;

            void test11();

    public:

            testClass1() : Fred(0), Judy(0) {}
            testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
           ~testClass1() {}

            testClass1 & operator=( const testClass1 & ) { return *this; }
            void test1();
            void test2();
    virtual void test12();
    virtual void test13();
};

inline void testClass1::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

inline void testClass1::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    testClass1 * testPtr = &tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    testPtr->test13();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test1()
{
    testClass1 tester;

    tester.test11();
    tester.test13();
}

void testClass1::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    test13();

    return;
}

Phase 8

This is the test framework modified to show refactoring by inheritance.

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class testClass0
{
    protected:

            int Fred;
            int Judy;

    public:

            testClass0() : Fred(0), Judy(0) {}
            testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
           ~testClass0() {}

            testClass0 & operator=( const testClass0 & ) { return *this; }
    virtual void test13();
};

class testClass1 : public testClass0
{
    private:

            void test11();

    public:

            testClass1() {}
            testClass1( const testClass1 & ) {}
           ~testClass1() {}

            testClass1 & operator=( const testClass1 & ) { return *this; }
            void test1();
            void test2();
    virtual void test12();
};

inline void testClass1::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

inline void testClass0::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    testClass1 * testPtr = &tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    testPtr->test13();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

testClass1 tester;

void testClass1::test1()
{
    test11();
    test13();
}

void testClass1::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    test13();

    return;
}

Phase 9

This is the test framework modified to show refactoring by composition.

The C++ Files

The test framework still has four files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class testClass0
{
    public:

            int Fred;
            int Judy;

            testClass0() : Fred(0), Judy(0) {}
            testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
           ~testClass0() {}

            testClass0 & operator=( const testClass0 & ) { return *this; }
    virtual void test13();
};

class testClass1
{
    private:

            testClass0 * tester;
            void         test11();

    public:

            testClass1() : tester(new testClass0) {}
            testClass1( const testClass1 & ) : tester(new testClass0) {}
           ~testClass1() {}

            testClass1 & operator=( const testClass1 & ) { return *this; }
            void test1();
            void test2();
    virtual void test12();
    virtual void test13() { tester->test13(); }
};

inline void testClass1::test12()
{
    int Judy = tester->Judy;
    int Fred = tester->Fred;

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

inline void testClass0::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1 tester1;
    testClass1 * testPtr = &tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    testPtr->test13();

    return( EXIT_SUCCESS );
}

File CPPtest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

testClass1 tester;

void testClass1::test1()
{
    test11();
    test13();
}

void testClass1::test11()
{
    int Judy = tester->Judy;
    int Fred = tester->Fred;

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

void testClass1::test2()
{
    int Judy = tester->Judy;
    int Fred = tester->Fred;

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    test13();

    return;
}

Phase 10

This is the test framework restructured to use a class template.

The C++ Files

The test framework now has two files:

  1. test.hpp
  2. CPPmain.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

template<class T> class testClass1
{
    private:

    T Fred;
    T Judy;

    void test11();

    public:

    testClass1() : Fred(0), Judy(0) {}
    testClass1( const testClass1 & ) : Fred(0), Judy(0) {}
   ~testClass1() {}

    void operator=( const testClass1 & ) {}
    void test1();
    void test2();
    void test12();
    void test13();
};

template<class T> void testClass1<T>::test1()
{
    test11();
    test13();
}

template<class T> void testClass1<T>::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

template<class T> void testClass1<T>::test12()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

template<class T> void testClass1<T>::test13()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

template<class T> void testClass1<T>::test2()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

template<class T> void test3( T inFred )
{
    T Fred = inFred;
    T Judy = 0;

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass1<int> tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();
    test3( 0 );

    return( EXIT_SUCCESS );
}

Phase 11

This is the templatized test framework refactored using inheritance.

The C++ Files

The test framework now has six files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp
  5. CPPtest3.cpp
  6. CPPtest4.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class baseTest
{
    private:

    int Fred;
    int Judy;
 
    public:

               baseTest() : Fred(0), Judy(0) {}
               baseTest( const baseTest & ) : Fred(0), Judy(0) {}
    virtual   ~baseTest() {}

               void operator=( const baseTest & ) {}
               void test11();
};

template<class T> class testClass0 : public baseTest
{
    protected:

    T Fred;
    T Judy;

    public:

    testClass0() : Fred(0), Judy(0) {}
    testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
   ~testClass0() {}

    void operator=( const testClass0 & ) {}
    void test12();
};

template<class T> class testClass1 : public testClass0<T>
{
    public:

    testClass1() {}
    testClass1( const testClass1 & ) {}
   ~testClass1() {}

    void operator=( const testClass1 & ) {}
    void test13();
};

template<class T> class testClass2 : public testClass1<T>
{
    public:

    testClass2() {}
    testClass2( const testClass2 & ) {}
   ~testClass2() {}

    void operator=( const testClass2 & ) {}
    void test1();
    void test2();
};

template<class T> void testClass0<T>::test12()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass1<T>::test13()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass2<T>::test1()
{
    test11();
    test13();
}

template<class T> void testClass2<T>::test2()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    test13();

    return;
}

template<class T> void test3( T inFred )
{
    T Fred = inFred;
    T Judy = 0;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

Note: baseTest is an example of a non-template base class whose member function is compiled in its own module. It is somewhat artificial because the templatized data members have been replaced with data members of a fixed type. In a real program, this would not usually be done; instead, code which did not depend on the template parameters would be identified and placed in such a class.

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass2<int> tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();
    test3( 0 );

    return( EXIT_SUCCESS );
}

File CPPTest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

template<class T> void testClass1<T>::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass0<int> tester0;

File CPPtest3.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass1<int> tester1;

File CPPtest4.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

void doTest3()
{
    test3( 5 );
    return;
}

Phase 12

This is the templatized test framework refactored using composition.

The C++ Files

The test framework still has six files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp
  5. CPPtest3.cpp
  6. CPPtest4.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class baseTest
{
    private:

    int Fred;
    int Judy;
 
    public:

    baseTest() : Fred(0), Judy(0) {}
    baseTest( const baseTest & ) : Fred(0), Judy(0) {}
   ~baseTest() {}

    void operator=( const baseTest & ) {}
    void test11();
};

template<class T> class testClass0
{
    private:

    baseTest localTest;

    public:

    T Fred;
    T Judy;

    testClass0() : Fred(0), Judy(0) {}
    testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
   ~testClass0() {}

    void operator=( const testClass0 & ) {}
    void test11() { localTest.test11(); }
    void test12();
};

template<class T> class testClass1 
{
    public:

    testClass0<T>   localTest;

    testClass1() {}
    testClass1( const testClass1 & ) {}
   ~testClass1() {}

    void operator=( const testClass1 & ) {}
    void test11() { localTest.test11(); }
    void test12() { localTest.test11(); }
    void test13();
};

template<class T> class testClass2
{
    private:

    testClass1<T>   localTest;
    
    public:

    testClass2() {}
    testClass2( const testClass2 & ) {}
   ~testClass2() {}

    void operator=( const testClass2 & ) {}
    void test1();
    void test11() { localTest.test11(); }
    void test12() { localTest.test11(); }
    void test13() { localTest.test11(); }
    void test2();
};

template<class T> void testClass0<T>::test12()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass1<T>::test13()
{
    T Fred = localTest.Fred;
    T Judy = localTest.Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    localTest.Fred = Fred;
    localTest.Judy = Judy;

    return;
}

template<class T> void testClass2<T>::test1()
{
    test11();
    test13();
}

template<class T> void testClass2<T>::test2()
{
    T Fred = localTest.localTest.Fred;
    T Judy = localTest.localTest.Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    localTest.localTest.Fred = Fred;
    localTest.localTest.Judy = Judy;

    test13();

    return;
}

template<class T> void test3( T inFred )
{
    T Fred = inFred;
    T Judy = 0;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

Note: baseTest is still a non-template class, in this case illustrating that such classes can be mixed with class templates by composition as well as by inheritance.

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include "test.hpp"

int main()
{
    testClass2<int> tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();
    test3( 0 );

    return( EXIT_SUCCESS );
}

File CPPTest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

template<class T> void testClass1<T>::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass0<int> tester0;

File CPPtest3.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass1<int> tester1;

File CPPtest4.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

void doTest3()
{
    test3( 5 );
    return;
}

Phase 13

This is the Phase 11 version of the test framework modified to use the header <string>.

The C++ Files

The test framework now has seven files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp
  5. CPPtest3.cpp
  6. CPPtest4.cpp
  7. CPPtest5.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class baseTest
{
    private:

    int Fred;
    int Judy;
 
    public:

               baseTest() : Fred(0), Judy(0) {}
               baseTest( const baseTest & ) : Fred(0), Judy(0) {}
    virtual   ~baseTest() {}

               void operator=( const baseTest & ) {}
               void test11();
};

template<class T> class testClass0 : public baseTest
{
    protected:

    T Fred;
    T Judy;

    public:

    testClass0() : Fred(0), Judy(0) {}
    testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
   ~testClass0() {}

    void operator=( const testClass0 & ) {}
    void test12();
};

template<class T> class testClass1 : public testClass0<T>
{
    public:

    testClass1() {}
    testClass1( const testClass1 & ) {}
   ~testClass1() {}

    void operator=( const testClass1 & ) {}
    void test13();
};

template<class T> class testClass2 : public testClass1<T>
{
    public:

    testClass2() {}
    testClass2( const testClass2 & ) {}
   ~testClass2() {}

    void operator=( const testClass2 & ) {}
    void test1();
    void test2();
};

template<class T> void testClass0<T>::test12()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass1<T>::test13()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass2<T>::test1()
{
    test11();
    test13();
}

template<class T> void testClass2<T>::test2()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    test13();

    return;
}

template<class T> void test3( T inFred )
{
    T Fred = inFred;
    T Judy = 0;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include <string>
#include "test.hpp"

int main()
{
    std::string     testString;
    testClass2<int> tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();
    test3( 0 );

    return( EXIT_SUCCESS );
}

File CPPTest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

template<class T> void testClass1<T>::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass0<int> tester0;

File CPPtest3.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass1<int> tester1;

File CPPtest4.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

void doTest3()
{
    test3( 5 );
    return;
}

File CPPtest5.cpp:

// This is a test function for the tutorial.

#include <string>

Phase 14

This is the Phase 13 version of the test framework modified to show what works with Open Watcom version 1.8.

The C++ Files

The test framework still has seven files:

  1. test.hpp
  2. CPPmain.cpp
  3. CPPtest1.cpp
  4. CPPtest2.cpp
  5. CPPtest3.cpp
  6. CPPtest4.cpp
  7. CPPtest5.cpp

File test.hpp:

// This is the header for the test class.

#include <stdio.h>

class baseTest
{
    private:

    int Fred;
    int Judy;
 
    public:

               baseTest() : Fred(0), Judy(0) {}
               baseTest( const baseTest & ) : Fred(0), Judy(0) {}
    virtual   ~baseTest() {}

               void operator=( const baseTest & ) {}
               void test11();
};

template<class T> class testClass0 : public baseTest
{
    protected:

    T Fred;
    T Judy;

    public:

    testClass0() : Fred(0), Judy(0) {}
    testClass0( const testClass0 & ) : Fred(0), Judy(0) {}
   ~testClass0() {}

    void operator=( const testClass0 & ) {}
    void test12();
};

template<class T> class testClass1 : public testClass0<T>
{
    public:

    testClass1() {}
    testClass1( const testClass1 & ) {}
   ~testClass1() {}

    void operator=( const testClass1 & ) {}
    void test13();
};

template<class T> class testClass2 : public testClass1<T>
{
    public:

    testClass2() {}
    testClass2( const testClass2 & ) {}
   ~testClass2() {}

    void operator=( const testClass2 & ) {}
    void test1();
    void test2();
};

template<class T> void testClass0<T>::test12()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass1<T>::test13()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    return;
}

template<class T> void testClass2<T>::test1()
{
    test11();
    test13();
}

template<class T> void testClass2<T>::test2()
{
    T Fred = this->Fred;
    T Judy = this->Judy;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    this->Fred = Fred;
    this->Judy = Judy;

    test13();

    return;
}

template<class T> void test3( T inFred )
{
    T Fred = inFred;
    T Judy = 0;

// 2000 reps. 

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPmain.cpp:

// The driver for the test program for the tutorial.

#include <stdlib.h>
#include <string>
#include "test.hpp"

int main()
{
    std::string     testString;
    testClass2<int> tester1;
    tester1.test1();
    tester1.test2();
    tester1.test12();
    tester1.test13();
    test3( 0 );

    return( EXIT_SUCCESS );
}

File CPPTest1.cpp:

// This is a test function for the tutorial.

#include <stdio.h>
#include "test.hpp"

template<class T> void testClass1<T>::test11()
{

// 2000 reps.

// 1000 reps.

// 100 reps.

// 10 reps.

    Judy += ++Fred;
    putchar(Judy);

    <1999 additional repetitions>

    return;
}

File CPPtest2.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass0<int> tester0;

void test0()
{
    tester0 test0;
    test0.test12();
}

File CPPtest3.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

typedef testClass1<int> tester1;

void test1()
{
    tester1 test1;
    test1.test13();
}

File CPPtest4.cpp:

// This is a test function for the tutorial.

#include "test.hpp"

void doTest3()
{
    test3( 5 );
    return;
}

File CPPtest5.cpp:

// This is a test function for the tutorial.

#include <string>
Personal tools