Script to automatically execute multiple runs of a c++ program - c++

Consider a basic program - SumOf2Numbers.cpp. I can give 2 number as input through command line and it give the sum of the number.
I want to run this program with various inputs like,
./a.out 5 6
./a.out 123456 654321
./a.out -200 200
And the output would be,
5 + 6 = 11
-200 + 200 = 0
123456+654321 = 777777
I want to automate this process of executing the c++ code and storing the output in a file. I am new to writing scripts. I would like to know how I can do this ? I believe I can do this by writing perl or bash scripts. Can someone guide me to a nice tutorial on this.
PS: I am sure there would be lot of online tutorials. But I am not sure how exactly I should perform the search.

This can be done easily with a shell script:
#!/bin/sh
(
./a.out 5 6
./a.out 123456 654321
./a.out -200 200
) > output.txt

Related

Give input to gdb debugger script

I want to pass some inputs to my gdb debugger to automate it.
Something like this:
Enter number of inputs:
5
Enter 5 inputs:
2 4 3 2 5
I have an expect script for that to automate my binary file.
Can I combine my expect script and gdb script?
Create a file testinput.txt containing:
5
2 4 3 5 2
Then in gdb:
(gdb) run < testinput.txt

looping over different list files, retrieving always the sam Eline

I have three files with a list of files I need to use later to apply a function.
I have managed to create a loop going through the 3 different files, but no exactly the output I need.
My input:
rw_sorted_test.txt
1
2
3
fwd_sorted_test.txt
A
B
C
run_list.txt
1st
2nd
3rd
I am running it like this:
for f in `cat rw_sorted_test.txt`; do for l in `cat fwd_sorted_test.txt`; do for r in `cat run_list.txt` do echo ${f} ${l} ${r}; done; done; done;
What I am obtain now is something like:
1 A 1st
1 A 2nd
1 A 3rd
2 A 1st
2 A 2nd
2 A 3rd
3 A 1st
(...)
What I am looking for is something like:
1 A 1st
2 B 2nd
3 C 3rd
I am sure that it will be something simple, but I am really beginner and all the workarounds have not been working.
Also, how can I then make it run after echo my desired output?
Thank you
Quick try, if it is this that you need:
exec 4< run_list.txt
exec 5< rw_sorted_test.txt
for a in $(cat fwd_sorted_test.txt); do
read run <&4
read sort <&5
echo "$sort $a $run"
done
...output is:
1 1st A
2 2nd B
3 3rd C
Files should also be closed:
exec 4<&-
exec 5<&-
The whole point is to do a single cycle, and read a line at a time from 3 different files. The files which are opened for input (exec ...< ...) should contain at least the same number of lines as the main file, which is controlling the loop.
Some reference could be find here: How do file descriptors work?
or doing some study on bash file descriptors. Hope it helps.
You can use this as a testcase for writing a program in awk:
3 inputfiles, store lines in an array and print everything in an END-block.
In this case you can use another program that works even easier:
paste -d" " rw_sorted_test.txt fwd_sorted_test.txt run_list.txt

how to merge odd number line and even number line in vim?

In order to merge the odd number line and the even number line in two methods.
One use command :s, the other use command :g and :s.
It's our homework and I could not get appropriate answer from the google.
And I had worked out the first one, which means I can solve it with command :s:
:%s/\(^.*$\)\n\(^.*$\)/\1 \2
And how could I use command :d and :s to solve it?
BEFORE:
1 aa
2 bb
3 abc
4 abc
5 an apple
6 is a bug
7 mazic
8 homework!
9 try a time
10 dodo
AFTER:
1 aa bb
2 abc abc
3 an apple is a bug
4 mazic homework!
5 try a time dodo
thanks to everyone and I have leant about how to solve it before the lesson.hah
:g/\(^.*$\)\n\(^.*$\)/s//\1 \2
What you can do here is :
Move the cursor to the line number to which you want to append the next line and then type below command in normal mode.
:s/\n/ /
Another way is go to the particular line and press SHIFT+V and then type below command:
:'<,'>s/\n/, /
Note that when you are in visual mode and press : then :'<,'> will automatically get typed. You just need to type regex ahead of that.
In both the above commands, g is not needed as it will not do any impact because only one \n will be there for each line.
You don't need to use :substitute here, there's a special command :join.
You can use the Ex command with :global, using ^ as the pattern to match all lines:
:global/^/join
Or use the shorter normal mode variant J:
:%normal! J

Convert paged columns to rows with a regular expressions

So first a sample of the actual data mangled (data is originally a mix of text and numbers, there's no significance to any of the data at this point and some of the patterns are just because I replaced most of the characters with 0s, 1s and Zs because the random number generator in my brain is broken):
011.0ZN1ZZ 001.F5ZS1Z 001.ZO5ZY0
014.5ZZZ1Z 001.1SZZOZ 001.ZLMZY0
016.01NM1SU54 001.EX0Z1Z 001.LIZZOZ
018.01NM1SS41 001.F83Z1Z 001.0011M1SU54
014.ZZ1YZZ 001.ZZZ1IZ 001.0011M1SS41
013.2EBSIZ 001.ZZZ11Z 001.0011SE4
01N.ZINSIZ 001.ZZZZ1Z P01.ZZZZ1Z
01N.01NSE4 001.LSZZHG N01.ZZZZ1Z
001.01ON5O 001.5Z21OL F01.ZZZZ1Z
001.NE5ZO1 001.ZOM05O D01.ZZZZ1Z
001.ZO5ZOZ 001.01NO1G Z01.ZZZZ1Z
001.ZO5ZOZ 001.01NO1G Z01.ZZZZ1Z
001.011ZOZ 001.01NZ0Y
Some additional comments.. I can clean up whitespace and deal with record length with no issues, so I'd like to simplify the question to this, I'm just including the above in case there's a solution to the simplified version that can't be easily extended to a more complex version.
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
19 25
20 26
21 27
22 28
23 29
24
So there will be a variable number of pages, but the same number of columns and rows on each page (although, in case it matters significantly, it's actually 12x3 instead of 6x3 but I wanted to keep it simple if possible), although the last page may be some empty rows/columns.
I'm using notepad++ but I have access to various gnutilities so if there's a solution that's way, way better than a regular expression I don't mind, although since I'll be using this a lot and use notepad++ a lot I'd appreciate a regex solution if it isn't too insane.
If you've got Git installed on your Windows machine, you may use Perl bundled with it from Git bash. Provided your input file is named data, try the following command (caution: it will orverwrite the input file):
echo >>data ; \
perl -i -lane'
$i=0;
push #{$c[$i++]}, $_ foreach #F;
if (/^\s*$/) {
push #l, #{$_} foreach #c;
print "#l\015";
#l=#c=();
}' data
The Perl command treats each line of input as space delimited fields and accumulates the fields in the #c matrix. When encounters an empty line (if (/^\s*$/) ...), it prints the matrix columns concatenated in a list.
The input file is changed in-place. A backup copy data.bak is created.
The input file may not end with an empty line so I add one with echo >>data. This makes the Perl script shorter and easier.
Another trick is the trailing \015 in print "#l\015";. This allows us to get Windows CRLF line endings in Unix-flavoured Git bash environment.
A demo can be found here: https://ideone.com/vnYoOd. But since Ideone forbids file read/write, the original command has been modified to make the code run there.

c++ testing with input cases from text files

How can i test a c++ program with given input test cases?
For example an input like this
23 45 78 45 12 67 23 76 56 34
34 65 78 45 74 3 98 34 23 97
I want to be able to input these numbers from a text file into a program at run-time and test expected output. I cannot input thousands of numbers by hand in a console so are there any softwares that allow these kind of testing?
This is already being used in InterviewStreet, they use given test cases and match the expected output to test a program.
--Edit--
Is there any way i can pass values from a text file into stdin ?
You could write a little bash script to run all of your tests. An individual test would look something like this:
#!/bin/bash
testprog < input1.txt > output1.txt
diff expected_output1.txt output1.txt
# examine the return code of diff to see if the files are different?