Write a shell script for automatic testing of c language programs

At present, some C language programs are often written in vim. After writing the program, some tests are generally required. Of course, we can perform some routine manual tests. I thought to myself, it would be great if I could use a shell script to write a program that can automatically test the C language.

In order to try this idea, I found a c language program topic:

[A ball falls freely from a height of 100 meters, and bounces back to half of its original height each time it hits the ground; when it falls again, how many meters does it pass when it hits the ground for the 10th time? How high is the 10th rebound?]

According to such a request, I wrote a program to solve this problem:

#include <stdio.h>
#include <stdlib.h>
#define H 100

int main(int argc, char* argv[])
{
    float h0=H;
    float sum=H;
    float h=h0;
    int count=2;
    int Number=atoi(argv[1]);
    for(;count<=Number;count++)
    {
  
        h=h/2.0;
        sum+=2*h;
    }
    h=h/2.0;
    printf("the initial height is: %d\n", Number);
    printf("the length is %.3f, the height is %.2f\n", sum, h);
    return 0;
}
This program can get the correct results. Below I wrote a shell script program for automatic testing.

#!/bin/bash


for((i=1;i<=10;i=i+1))
do
    ./a.out $i
done
First execute cc *.cpp on the terminal under the mac system, so that the a.out executable program is generated, and ten sets of examples are tested in this shell script:

file:///Users/daidapeng/Desktop/screenshot%202015-07-10%20pm 11.38.01.png




Guess you like

Origin blog.csdn.net/daida2008/article/details/46836217