dimanche 10 octobre 2021

Bash compare two Integers

I am trying to Automate running a CPP Program. Here's what I've tried.

compilation_log=$(g++ -std=c++17 -Wshadow -Wall -o exe $1 -O2 -Wno-unused-result)
start=`date +%s.%6N`
timeout 2s ./exe < STDIN > STDOUT 2> STDERR
end=`date +%s.%6N`
runtime=$( echo "$end - $start" | bc -l )
echo $runtime
if [[ ( $runtime -gt 2 )]]
then
    printf "Time Limit Exceeded\n"
fi

This doesn't work for programs that are running for more than 2s. For example, the following CPP script runs indefinitely. So, timeout halts this program as soon as the execution time exceeds 2s.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n = 1;
    while(n >= 0) {
        n /= 2;
    }
    return 0;
}

The echo $runtime statement is printing a float value greater than 2.0, but the control doesn't get into the then block.

Please let me get this work, thanks in advance.

Aucun commentaire:

Enregistrer un commentaire