Search code examples
c++stdinstress-testing

how do i configure `stdin` to read from a c++ string?


consider a cpp function solve(). A string a of numbers (sample test string) is passed to the function solve() as arguments. I wanted stdin to read the numbers in the string a.

It is actually a stress-test. So in this stress test, this function solve() is provided with a string to return a result which will be tested against another result which is obtained from another function solveFast().

NOTE:- Algorithm in the function solve() is already given to us. I wish to stress-test this algorithm against my own algorithm (in solveFast()). It is guaranteed that Algorithm in the function solve() provides the correct output against its test inputs

#include <bits/stdc++.h>

using namespace std;

int solve(string s) {
    // below given: an algorithm that 
    //uses stdin to take input sample test case (i.e.,not string s)
    int n; //number of integers in string s

    //ignore the purpose of the code below. 
    //Just observe that it is taking the inputs as cin (not from string s.
    cin >> n;
    int first, second, large; 
    for (int i = 0; i < n - 1; i++) {
        if (i == 0) {
            cin >> first >> second;
            large = (second > first) ? second : first;
            if (second < first) first = second;
        } else {
            cin >> second;  // new num
            if (second > large) {
                first = large;
                large = second;
            }
            else if(second > first){
                first = second;
            }
        }
    }
    int result = large * first;

    return result;
}
int solveFast(string s) {
    
    /*
     * my solution here
     */

    return result;
}

int32_t main() {

    //stress-testing code starts here
    while (true) {
        
        string a;

        /*
         * generating a sample test case and storing it in a string 'a'
         */

        int res1 = solve(a);
        int res2 = solveFast(a);
        if(res1!=res2){
            cout << "Wrong answer: " << res1 << " " << res2 << endl;
            break;
        }
        else{
            cout << "OK\n";
        }
    }
    //stress-testig code ends

    /*
    for (int i = 1; i <= t; ++i) {
        int n;
        cin >> n;
        vector<int> numbers(n);
        for (int i = 0; i < n; i++) {
            cin >> numbers[i];
        }
        auto result = solve(numbers);
        cout << result << endl;
    }
    */

    return 0;
}


Solution

  • Pass a std::istream to your function and then construct a istream from a string. As a general rule, do not to use global variables (like std::cin) in code that is meant to be unit-tested.

    #include <sstream>
    #include <iostream>
    int solve(std::istream& input, std::string s) {
         std::string variable;
         input >> variable;
         return 0;
    }
    void unit_test_solve() {
         std::istringstream input("some string");
         solve(input, "bla");
    }
    int real_code() {
         solve(std::cin, "bla");
    }