Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
- wrote 4 new tests
- seggregated I/O of test programs
- partially fixes #2
  • Loading branch information
Bhupesh-V committed Dec 18, 2019
1 parent a89b6d9 commit 8e7f16f
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 90 deletions.
2 changes: 1 addition & 1 deletion coderunner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Import Run() for initializing program data"""
from coderunner.coderunner import Run
from coderunner.coderunner import code
29 changes: 18 additions & 11 deletions coderunner/coderunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
FIELDS = "?fields=stdout,memory,time,status,stderr,exit_code,created_at"


class Run:
class code:
"""
Args:
- Source Code
Expand Down Expand Up @@ -101,12 +101,12 @@ def __readStatus(self, token: str):
return self.__response["status"]["description"]

def __submit(self):
if self.inp is not None:
api_params["stdin"] = self.inp
if self.program_input is not None:
api_params["stdin"] = self.program_input

api_params["expected_output"] = self.output
api_params["expected_output"] = self.program_output
api_params["language_id"] = self.language_id
api_params["source_code"] = self.source
api_params["source_code"] = self.source_code

res = requests.post(API_URL, data=api_params)
token = res.json()
Expand Down Expand Up @@ -150,17 +150,24 @@ def getTime(self):
"""
return self.__time

def getStatus(self):
def run(self):
"""
submit the source code on judge0's server & return status
submit the source code on judge0's server
"""
print(self.path)
print(self.inp)
print(self.source)
print(self.output)
if self.path:
if self.inp is not None:
self.inp = self.__readStandardInput()
self.source = self.__readCode()
self.output = self.__readExpectedOutput()
self.program_input = self.__readStandardInput()
self.source_code = self.__readCode()
self.program_output = self.__readExpectedOutput()

token = self.__submit()
status = self.__readStatus(token)
self.__token = token

def getStatus(self):
status = self.__readStatus(self.__token)

return status
29 changes: 20 additions & 9 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
from coderunner import coderunner
import pprint

program_name = "testfiles/" + "test_python.py"
source_code = "testfiles/" + "test_python_input.py"
language = "Python"
output = "testfiles/" + "output2.txt"
Input = "testfiles/" + "input.txt"
r = coderunner.Run(program_name, language, output, Input)
output = "testfiles/output/" + "output2.txt"
Input = "testfiles/input/" + "input.txt"
r = coderunner.code(source_code, language, output, Input)

# run the code
r.run()

# get Submission status
print("Status : " + r.getStatus())
if r.getError() != None:
pprint.pprint("Error : " + r.getError())

r.run()

# get Submission status
print("Status : " + r.getStatus)

# check if any error occured
if r.getError() is not None:
pprint.pprint("Error : " + r.getError())
else:
print("Standard Output : ")
pprint.pprint(r.getOutput())
print("Standard Output : ")
pprint.pprint(r.getOutput())
print("Execution Time : " + r.getTime())
print("Memory : " + str(r.getMemory()))
print("Memory : " + str(r.getMemory()))
1 change: 0 additions & 1 deletion testfiles/input.txt

This file was deleted.

1 change: 1 addition & 0 deletions testfiles/input/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
232
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions testfiles/input/input4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Shruti
50000
File renamed without changes.
1 change: 1 addition & 0 deletions testfiles/output/output2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The number is a palindrome!
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions testfiles/output/output5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Not eligible for loan!
1 change: 1 addition & 0 deletions testfiles/output/output6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
1 change: 1 addition & 0 deletions testfiles/output/output7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 11 2 3 5 8 13 21 34 55 89 144 233 377
1 change: 1 addition & 0 deletions testfiles/output/output8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Factorial = 362880
1 change: 0 additions & 1 deletion testfiles/output2.txt

This file was deleted.

31 changes: 15 additions & 16 deletions testfiles/test_c++.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <iostream>

using namespace std;
int main(){
int n, num, digit, rev = 0;
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << "The reverse of the number is: " << rev << endl;
if (n == rev)
cout << "The number is a palindrome.";
else
cout << "The number is not a palindrome.";
return 0;
// calculate factorial of a number
long Fact(long);

int main () {
long x = 9, fact;
fact = Fact (x);
cout << "Factorial = " << fact;

return 0;
}

long Fact (long a) {
if (a == 0) return 1;
else return a * Fact (a - 1);
}
19 changes: 19 additions & 0 deletions testfiles/test_c++_input.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
using namespace std;
int main(){
int n, num, digit, rev = 0;
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << "The reverse of the number is: " << rev << endl;
if (n == rev)
cout << "The number is a palindrome.";
else
cout << "The number is not a palindrome.";
return 0;
}
26 changes: 15 additions & 11 deletions testfiles/test_c.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include<stdio.h>

int main(){
int a, b, c;

scanf("%d%d", &a, &b);

c = a + b;

printf("Sum of the numbers = %d\n", c);

return 0;
}
int F1 = 0, F2 = 1, F3, n = 15;
if(n == 2) printf ("%d %d ", F1, F2);
else{
printf("%d %d", F1, F2);
do{
F3 = F1 + F2;
printf("%d ", F3);
F1 = F2;
F2 = F3;
n--;
}while(n > 2);
}
return 0;
}
13 changes: 13 additions & 0 deletions testfiles/test_c_input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include<stdio.h>

int main(){
int a, b, c;

scanf("%d%d", &a, &b);

c = a + b;

printf("Sum of the numbers = %d\n", c);

return 0;
}
29 changes: 29 additions & 0 deletions testfiles/test_java_input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Scanner;
class Loan extends Exception {
String name;
double salary;
void getInfo () {
Scanner input = new Scanner (System.in);
name = input.nextLine ();
salary = input.nextDouble ();
}
void checkEligibility () throws Loan {
if (salary < 70000) throw new Loan ();
else System.out.println (name + " is eligible for loan!");
}
public String toString () {
return "Not eligible for loan!";
}
}
class Main {
public static void main(String[] args) {
Loan person = new Loan ();
person.getInfo ();
try {
person.checkEligibility ();
}
catch (Loan l) {
System.out.println (l);
}
}
}
3 changes: 1 addition & 2 deletions testfiles/test_python.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
name = str(input())
print("Hello, " + name)
print("Hello, World!")
11 changes: 11 additions & 0 deletions testfiles/test_python_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
n = int(input())
temp = n
rev = 0
while(n > 0):
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if(temp == rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Loading

0 comments on commit 8e7f16f

Please sign in to comment.