Program Interaction and testing via bash script - c++

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.

Related

How to run a C++ Program interactive using external tools in gedit

I'm trying to run my C++ program interactively straight in gedit using external tools.
I've already written a Python tool for compiling it, but when using os.system("./program.out") all input for the program is set to 0 (but it is getting executed right, though).
Since using Python for executing it interactive would be rather difficult, im looking for another solution.
#!/usr/bin/env python3
import os
filename = os.getenv("GEDIT_CURRENT_DOCUMENT_NAME")
fout = "%s%s"%(filename[:len(filename)-4], ".out") #
os.system("c++ -o %s %s" % (fout,filename))
os.system("./%s" % fout)
the compiling works flawless, but ./program.out also doesn't give the wanted result using bash.
Any suggestions?
I guess i've found an answer. It may not be the most beautiful solution, but it works.
When defining a tool you can choose the input in a drop-down menu, there is an option for selected text as input.
So for example, if you have a program that reads 3 variables from stdin, you can add a comment to your code like //1 3 2, only select the numbers and then run your tool.
These numbers will be used as input in this order, somehow it only works with spaces.
A cruel way of implementing this would be to scan for input calls in the source code and ask for them via zenity in the gedit tool, i guess.
And here is another solution:
I can just use echo 3 2 1 | ./program.out in the tool
this makes everything way more easy

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.

Creating and running a file with command line arguments

I am very new to linux and apologize if my descriptions are not savvy. I will try to be relevantly detailed.
Currently I am working on a terminal using Fedora, and my goal is to create a smaller data set to run a program. I was given an example, and my mentor said that to run the program all I had to do was type "./filename" into the console.
filename has command line arguments as follows: "./main ./textfile1 ./textfile2" Basically, each argument is separated by a space.
I tried recreating this document with similar format, but I am not sure what to save it as, nor does it work when I try running it the same way as the file with a larger data set.
Also, filename is bold in the terminal, whereas the document I created it is not. I'm not sure if this helps at all, but it is a difference I noticed.
Any help would be appreciated.
You need to set the execute bit on your file.
chmod +x filename
Make sure you compile the program first (in case you haven't. I use the g++ compiler typically) and then use the ./filename like your instructor said, but do not put "./" in front of the arguments. Just write it as "./filename textfile1.txt textfile2.txt"

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

supplying inputs to stdin programatically line by line?

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.