-2

I have a directory that looks like this:

.
├── makefile
├── solution
│   ├── weather.cpp
│   └── weather.h
├── student
│   ├── weather.cpp
│   └── weather.h
└── tests
    └── test_Weather_Weather.cpp

solution/weather.h

#ifndef _WEATHER_H_
#define _WEATHER_H_

#include <string>
using namespace std;

class Weather {
    private:
        int temp;

    public:
        Weather();
        string announce();
};

#endif

solution/weather.cpp

#include <iostream>
#include "weather.h"

using namespace std;

Weather::Weather() {
    temp = 0;
}

string Weather::announce() {
    if (temp <= 0) {
        return "It's freezing!";

    } else {
        return "It's hot!";
    }
}

What I'm trying to accomplish is that I want to compile and run test_Weather_Weather.cpp, which will catch a bug in the default constructor of Weather in the student definition. I want to unit test this constructor (and eventually announce), so I want to use the solution symbols for everything except the student constructor.

I want to be able to accomplish this at compile time, aka before compiling I want both student and solution source/header files to be exactly the same (aside from their definition differences).

I have tried to get it to work using g++ -D flag, but I've been unsuccessful.

jajabarr
  • 551
  • 2
  • 5
  • 19
  • What is the relationship between "solution" and "student":? Without that this question makes no sense. – John3136 Nov 25 '18 at 21:52
  • I want to unit test implementations. I mentioned this in the post, but I just want to be able to use and call Weather from the test file, but have it's constructor use the student definition, and everything else be from the solution (i.e., announce) – jajabarr Nov 25 '18 at 21:57
  • Yeah, but you have 2 versions of weather.h and weather.cpp and one test. What is the relationship between the 2 versions of weather? – John3136 Nov 25 '18 at 22:00
  • I'm sorry I'm just having trouble really understanding your question. The items in the solution dir represent correct implementations. The items in student dir represent possibly buggy implementations. – jajabarr Nov 25 '18 at 22:07
  • "The items in the solution dir represent correct implementations" - how do you know that? –  Nov 25 '18 at 22:10
  • Ok. I think I understand what you are asking now. student is a "buggy" copy of solution with no other relationship. (Weather has nothing virtual so it can't be a subclass). You want some of solution and some of student to make the final exe? That sounds really messy and something to be avoided at all costs! To give a better answer than "Don't bother", we'd need to know **Why?** – John3136 Nov 25 '18 at 22:21
  • @NeilButterworth because in this situation I am defining what the correct implementation is, and providing a template for others to fill in. I want to be able to then run their filled-in definitions and verify their correctness. – jajabarr Nov 25 '18 at 23:31
  • @John3136 to be frank I'm a little shocked to see that this application appears to be strange. My motivation is that I have correct code A, and want to provide a blank (non-implemented) slate of that same code to others (B). I want them to fill it in, and then verify their correctness programatically. Therefore, I want a way to unit test methods in B, and the way I want to do this is by comparing their results to those from A. – jajabarr Nov 25 '18 at 23:35
  • @jajabarr The normal way of developing is to have one version of Weather and a set of tests for it. Perhaps not all of the tests pass at first. If you already have the right, passing, correct code why do you even need "student" ? I come back to **Why** ? Are you a tutor looking to have multiple "student" folders to compare student implementations to your known correct solution? Normally you'd just run your test cases against the student's implementation. Why do you need 2 copies? – John3136 Nov 25 '18 at 23:41
  • @John3136 that is one use yes, I would have multiple student folders and need to test each one. The reason I don't want to just write one test suite in this example is because (for a larger example) several functions may have dependence on one another. Then it's no longer a real unit test but rather a file? test I suppose. I want to truly isolate a function, plop it into the otherwise correct solution, and see in isolation what its correctness is. – jajabarr Nov 26 '18 at 00:09

1 Answers1

0

The problem with using compile-time defines in this case is that you'd need to clutter the student solution with test preprocessor flags. Instead, why not rename the methods in your solution class so the test file can call exactly what it wants. If both your solution class and the student class implement the same virtual methods, your test class can call both.

Paul
  • 370
  • 1
  • 6
  • Hmm I was hoping it may possible to compile the student implementation, and then link it when compiling the solution implementation, and just point the compiler to use the definition from student/weather.o when it sees it in the solution src file. Is this not possible? – jajabarr Nov 25 '18 at 22:09
  • It sounds like you're talking about weak linking. If the student implementation's methods are all marked as weak (see [link](https://stackoverflow.com/questions/15525537/what-are-practical-applications-of-weak-linking)), then you can use preprocessor definitions in your solution class to override only certain methods. – Paul Nov 25 '18 at 22:15