Testing a program

closed account (NCRLwA7f)
Hello,
I have written a program to identify local trees in the area. Basically all you have to do is choose between 2 questions (A or B) each question leads to a new question where you have to choose A or B finally after some time it will zero down on the correct tree identification. I want to test the program, is there a way that I can simulate user input to test all possible conditions to see if there is a broken link someplace.

short of actually taking the cin >> input statement out and hard coding a value, is there another more effective way to simulate user input?

this is basically what my program looks like, it's a switch statement for each type of tree group. $ feed into the function and the correct switch question is asked, when the user inputs an answer the "if" and "else if" decides where to go next.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

 void T::magnolias(int $){
	char input;
	switch($){
	case 1: doubleQuest(mag1A, mag1B);
			std::cin >> input;
			if(input == 'A')
				hardwoords(2);
			else if(input == 'B')
				hardwoords(3);
			break;
	case 2:	doubleQuest(mag2A, mag2B);
			std::cin >> input;
			if(input == 'A')
				get_names(MagnG);
			else if(input == 'B')
				get_names(MagnV);
			break;
	case 3:	doubleQuest(mag3A, mag3B);
			std::cin >> input;
			if(input == 'A')
				get_names(MagnA);
			else if(input == 'B')
				get_names(MagnM);
			break;
	}
}


this is one of the shorter switch statments, there are others with more that 20 case questions. I want to test every possible out come to see if the program works correctly.

Thank you
Last edited on
Just redirect a file with the responses in it.

Or write an auxilary program to generate the responses and pipe it into your main program.

BTW, I wonder if that $ is portable?
Last edited on
closed account (NCRLwA7f)
Thats a great idea, I was just wondering if there was something in c++ that could actually do that. none the less you gave me the idea to just replace cin with a bool function 0 = A, 1 = B it generates the letters randomly by modding rand() , I think looping it about 4000 times will hit every condition at least once.
could change your function to take istream&, and then you could pass it cin, an ifstream, an istringstream, etc.

btw, for your binary idea -- why use rand() when there's a countable amount of choices? Can generate every option one time with nested for loops or so.
Last edited on
closed account (NCRLwA7f)
icy1 yeah...I would like a bit more control. I have tried just random generating A or B and it does work fine. My program has 69 trees that can be Identified with the program. But it would be nice to decide what branch to follow v/s just randomly getting and output. Are you proposing maybe something like a truth table?

69 tree ID should mean that there are 69 possible branches to follow.

The Q and A can actually be drawn as a binary tree, but.....maybe for another day since it would be a binary tree of binary trees.....and I don't have too much time right now to tinker with it.

Now that I think about it 4000 loops is overkill.......
Last edited on
I was thinking more of the effect similar to Ruby
1
2
3
['a','b'].repeated_permutation(3).each {|r| puts r.join}
puts
['a','b','c'].repeated_permutation(3).each {|r| puts r.join}

runnable at https://repl.it/repls/SphericalTatteredInternet

Output is all permutations (with repetition) of the array. 3 would be the number of questions here, and the permutation is one simulation.

I think I've done it here in C++, recursively
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

const vector<string> CHOICES = {"a", "b"};

// Recursively builds all string possibilities
void GenerateResponses(string s, const int& MAX, vector<string>& responses)
{
    if (s.size()==MAX)
    {
        responses.push_back(s);
        return;
    }

    for (auto& c : CHOICES)
        GenerateResponses(s+c, MAX, responses);
}

// This function begins the recursive process with correct parameters
vector<string> AllResponses(int n)
{
    int sz = pow(CHOICES.size(), n);
    vector<string> responses;
    responses.reserve(sz);
    
    for (auto& c : CHOICES)
        GenerateResponses(c, n, responses);
    
    return responses;
}

int main()
{
    int num_questions = 3;
    vector<string> all_responses = AllResponses(num_questions);

    for (auto& r : all_responses)
        cout << r << endl;
    
    return 0;
}

https://repl.it/repls/SuburbanVioletBootstrapping

aaa
aab
aba
abb
baa
bab
bba
bbb

Last edited on
closed account (NCRLwA7f)
Since not all the ID's are going to be the same length, it might need a little modification but I see what you mean. This will also come in handy of my next little side project.

Thank you all for the help.
Topic archived. No new replies allowed.