supplying inputs to stdin programatically line by line? - c++

I have a test program which prompts for input from user(stdin), and depending on inputs, it asks for other inputs, which also need to be entered.
is there a way I can have a script do all this work ?

There's a program called expect that does pretty much exactly what you want -- you can script inputs and expected outputs and responses based on those outputs, as simple or complex as you need. See also the wikipedia entry for expect

I may have misunderstood, but do you have a program which reads the input and does something with it, and you just want to know how to automate providing it some test input?
For a given test case, does the input you provide have to depend on the output from the program, or is it the same every time?
If the input for a given test is the same every time, then just put it in a text file and redirect stdin for your program to read from that file:
myprogram.exe < input.txt
If the input is different each time, for the same test, then this doesn't help. But for a typical simple test, you just want to answer "y" to the first question, "n" to the second, and "hello world" to the third, or whatever.

In the general case, yes, there is.
For more specific tasks, you can get other tools to do the job that will be more specialized and usable for that particular task.

Related

Python: Grab text printed to console

I'm using a complicated python module called CAMFR. In one function, it calculates a value that I want to use (to plot or otherwise), but unfortunately the module prints this value to the python console but doesn't return it as a variable!
(I have poked through the source code to see if I can recompile the module to return the values, but this looks excessively difficult at my programming level, considering it's written in C++ and uses Boost etc. I just don't get it unfortunately.)
SO, option number two is to grab the text printed to the console and parse out the value I need.
How can I intercept or otherwise acquire this function's console output text in Python (2.7)? (I will RegEx it afterwards.)
Thanks!
Here is an example of the text printed to the Python console:
<<a lot of other output, and then at the end of the function:>>
...
# 1.05554 4.65843e-05 5.54592 0.0903205 1
# 1.05554 2.87907e-05 3.42757 0.0903205 1
# 1.05554 2.87907e-05 3.42757 0.0903205 1
Done pass 1: lambda 1.05554, gain 3.42757
I ultimately want to grab the Lambda=1.05544 & Gain=3.42757 values, for example, and shove them into variables. Grabbing the entire console output of this one function would allow me to do that via a subsequent RegEx search, so I'm looking for a way to do that.
I apologize if there is another thread with the answer I need, I could not figure out google search terms that got me what I needed. Thanks for your patience & generous help!

Looking for executable-based test framework

I've inherited an old program that works, but has pretty ugly source code. I need to make the code nicer without changing the functionality. This program takes an input file, performs all sorts of calculations and generates an output file.
The program is currently written in a C/C++ combination. At first I'm going to keep it a C++ program, but in the not-so-far future I'm going to convert it, or parts of it, to Python.
Naturally, the original developers haven't taken the time to create unit tests, or any other kind of test. Since I want to make sure my modifications haven't changed the program's behavior, I want to start by creating some tests. These will not be unit tests, but rather tests of the entire program.
I want each test to take one input file and a set of command line arguments, run the program and compare the output (which is the output file, stdout output and stderr output) to the expected output.
Since I need to support both C++ and Python, the test framework needs to be language agnostic - it should be able to run an executable, collect stdout and stderr and compare them, as well as another file, to the prerecorded outputs.
I couldn't find a test framework that can do that. Is there anything like that? I'd rather not develop one myself.
Well, off the top of my head, you could certainly run the executable with your desired inputs in python using subprocess or some similar module, parse the output and then use the unittest module to set expectations on what sort of output you're looking for.

Menu Program, Input string

I'm trying to create C++ menu program that does a lot of things such as count bytes, lines, words in a file.
I did all that. But what I could not do is to accept an mixed cases from the user. For example, if the user input "countbytes" the program will run but if he entered "CouNt Bytes" the program will not run.
So how could I do it?
If you do not want to change the logic that you have already developed, an easy solution is to take the input from the user and convert it into lower case. Then process it.

Reading output from the command line

I'm trying to write a simple GUI for Wget. I'm looking for advice on how to read information from the command line output that Wget generates when it is doing a run. I'd like to update that download information real time to a list box or some equivalent. The GUI will be in Visual Basic. I know programs like WinWget do this, and their source code is available, but I don't know the language that's written in well enough to find what I'm looking for.
tl;dr: I need to update a list box real time with command line output.
There are two ways to use the output of one console application for the input of an other:
The first way is to use the | operator; for example:
dir |more
The second way is to write the data into a file and process it later.
dir > data.txt

Program Interaction and testing via bash script

I've just completed the coding section of simple homework assignment for my C++ class. The second part of the assignment requires us to verify our code's input validation. (The program takes several different values as inputs from a user and prints those values to a file)
I was hoping that I could use bash script for this. Is there any way to use bash script to run and interact with a program? How can I put the program's output into a variable (note that program has a series of input requests and outputs).
Thanks
To build on #Travis' answer, create two files: one holds your inputs (input.txt) and one holds the expected output (expected_output.txt). Then do the following:
./myprogram <input.txt >output.txt
diff output.txt expected_output.txt
If the diff command has any output, there's a problem.
You can do much of this with a shell script but you might want to consider using some other testing tools instead like CppUnit or expect.