How do I develop test cases for this code? Like, you know, testing the methods. I don't really know. If there was a "return", I could do it that way, but there isn't. So, you make a skeleton of tests, then adjust each test so that it actually tests the method (the method should pass, since it already works). Does anybody know how I do this here?


#include <string>
#include <iosfwd>

/** Class to represent vote ElectionResults */
class ElectionResults {


/** Construct an empty ElectionResults */
ElectionResults(): num_candidates(0), num_precincts(0), vote_array(0) {}

* Load vote data from file
* @param filename String
* @return boolean True if successful, false otherwise
bool load_from_file(const std::string&);

* Manually load vote ElectionResults
* @param numPrecincts size_t
* @param numCandidates size_t
* @param voteArray int[][] Each row contains a candidate and each column
* represents a precinct
bool manual_load(size_t, size_t, int**);

* Edit a particular entry in the voting table
* @param precinct size_t
* @param candidate size_t
* @param numVotes int
bool edit_vote_data(size_t, size_t, int);

* Creates a table of raw voting results
* @param out The ostream to write the results
void generate_raw_table(std::ostream&);

* Generate a table of voting results in percentage format
* @param out The ostream to write the results
void generate_percentage_table(std::ostream&)…

* Retrieve the top candidates
* @param num size_t Number of candidates to retrieve
* @param out The ostream to write the results
void display_top_candidates(size_t, std::ostream&);

/** The number of candidates */
size_t num_candidates;

/** The number of precincts */
size_t num_precincts;

/** The array to contain the votes */
int** vote_array;



Edited by Lemonader: n/a

7 Years
Discussion Span
Last Post by Lemonader

If there was a "return", I could do it that way, but there isn't.

Create a class ElectionResults_test_driver .

Add a friend declaration friend class ElectionResults_test_driver ; to ElectionResults .

Let the test driver examine the innards of ElectionResults after each test case.


I don't know what this would check. I forgot to mention that the data file it loads from looks like this:

1 2 3 4 5
6 7 8 12 2

I'm using CppUNIT VissualAssert in Visual Studio 2010. Does it have some tool that would help here?

Edit: Never-mind, I don't think I have to consider the data file. that's irrelevant for the test cases.

Edited by Lemonader: n/a


What exactly are you trying to test?

If you expect a variable to be something but aren't sure if it will be that, you should use an ASSERT macro.

#define DEBUG // Get rid of this once you're done debugging.

#ifndef DEBUG 

#define ASSERT(x) 

#define ASSERT(x) \ //single forward slash continues macro to next line, don't forget it.
   if (x != 5)   \ //The code assumes x will be 5.
   { \ //Even brackets need them.
    cout << "Error at " << #x << endl \ // # gives the name of the variable,
    cout << "On line: "<< __LINE__ << " Of " << __FILE__<<endl;  \  // __LINE__ and __FILE__ gives the location.
  } //last line of the macro does not need a forward slash.
#endif //Don't forget to end your if statement.

This may seem silly and simplistic, however, you can make x a function and look for true or false, and by doing that, especially with classes, it can be a very powerful tool.

If this isn't what you're trying to do, could you explain a little clearer?


Well, I'm wanting to develop and run a test plan for ElectionResults class using CppUNIT VissualAssert in Visual Studio 2010. And I have not figured out how to write a test case for this ElectionResult file :(


Ok, maybe I was unclear about it.

I'm using Visual Studio to do this. You create a new project, and then you add new items to it. Then you run a test and it will create a test file. I have to test the methods of the code(load from file, manual load, edit vote). I'll have to modify the test file. I don't know how I can test the methods.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.