Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

test a program?

  Asked By: Fahimah    Date: May 23    Category: Java    Views: 1038
  

I need help and I hope someone in this forum willing to help me.
Right now i'm developing a small project for my school. My project
should be written in java, and this is the project description :
my project will act as a program tester (maybe something like 'black
box'). Say, a teacher ask his students to create a homework using
java/C++. The homework can be various, maybe a simple CUI data
structure or a program that using quite complex gui. The students
must then submit the .java or .cpp file for their homework. Well, my
project will be act as a tester. So my project input is the .cpp
or .class file, then my project will test the homework. If the source
file can't compiled, then it failed. Otherwise, my project will
assign maybe about 100 inputs for the homework, and produce 100
output (or maybe homework terminated incorrectly, or infinite loop,
or exceed execution time limit). Of course, the teacher has the
correct output for same 100 input. My project will compare the
output from the teacher and the student's homework. The grade for
students based on how may errors occured. Also, my project will
record and then display the errors (ex : "for input '12' and 'asd',
your output is '12asd', should be '45qwe').

The problem is i don't know where to start. Is it possible for my
project to get an input from, maybe text file or database, and send
the input to the student's homework at the runtime? If this can be
done, how?? Also, how can my project get the output from the homework
at the runtime?? Also, how can I invoke javac (for .java) or gcc
(for .cpp) and know wether the source files can be compiled?? Which
java technology should i use?? Can u give me an url where can I get
the tutorial, I tried sourceforge.net, but can't find similiar
project??

Share: 

 

4 Answers Found

 
Answer #1    Answered By: Kyle Fox     Answered On: May 23

Assuming you only really need to test  output from a desired set of
input, and only using CLI … you could probably just hack it together in
bash script in a few hours. If you really had to do it in java, you can
hijack a programs input  and output with ByteArrayInputStream and
ByteArrayOutputStream … it is pretty effective for that kind of testing.

The moment you want to do any kind of introspection on the class  file
(to see what it is really doing), or testing GUI components then … well
you’re not getting paid enough :)

So if all you really want to do is compare the desired output to the
actual output … not hard. If you want to compare the magical stuff
happening inside a class (include GUIs), then that just filled the too
hard basket.

Be warned though, students can break automatic testing like it is going
out of style, even when it works perfectly everywhere else but in the
automatic testing script. Also strict adherence to automatic output
testing doesn’t actually test what the programmer has actually written

Point in case in one of my programming assignments in first year
(program a linked list) got 80/100 even though he didn’t make a
linkedlist (he used the JFC one). His output was perfect and he followed
the coding standards properly. Of course that made his assignment about
5 hours of work, while every body else beat their heads against linked
lists for about 20 hours :) (I got 87% and I worked my arse off).

 
Answer #2    Answered By: Leonard Pierce     Answered On: May 23

Well, I don't understand it yet.
What do you mean by "hijack a programs input  and output ". Can you
give me a sample code?? Hope my first question clear enough. Anyway,
i think my project  will use only character UI, not a complex  GUI. And
these are the detailed steps :

1. Suppose the Homework program  has a function : int calculate(int x,
int y), which returns x * y. This is the function that must be tested
at runtime. The students must submit the Homework.java file
2. So how should my program works?? I guess my program must try to
compile Homework.java from each student (guess I can use this by
executing javac from Runtime). If Homework.class can created, then go
to step 3. Otherwise, Homework program failed.
3. My project then execute the Homework.class. Well, now
Homework.class is running. And this is the most confusing part. How
can i enter first input (for x), second input (for y) and gets the
output from "calculate" function from the running program?? If this
part you called as "hijack", how can i interrupt a running program??

Hope someone can help  me, cause this is really important to
me.........

 
Answer #3    Answered By: Elisabeth Bell     Answered On: May 23

> Well, I don't understand it yet.
> What do you mean by "hijack a programs input  and output ". Can you
> give me a sample code??

I mean that you can overwrite the standard input and output streams to
be read and write from another source ... such as your java  program.

You can use ByteArrayInputStream and ByteArrayOutputStream to do so.

I would love to give you a code example of how to do this in a really
basic way, but I am working 16 hour days at the moment :( ... Maybe
tomorrow (after my deadline).

> Hope my first question clear enough. Anyway,
> i think my project  will use only character UI, not a complex  GUI. And
> these are the detailed steps :
> 1. Suppose the Homework program  has a function : int calculate(int x,
> int y), which returns x * y. This is the function that must be tested
> at runtime. The students must submit the Homework.java file

Command Line Interfaces are by far the easier ones to do. GUI isn't easy
in any way shape or form. That being said, testing a few dozen CLI
programs submitted by students is going to be a huge headach. I have
seen students that get 97% on assignments fail almost every single test
for really bizzare reasons.

If you need to test  a method that is fixed for every students program,
you could use a script with Unit testing. I don't know what the link is
for c++ unit testing, but JUnit for Java is easy to find and does
exactly that.

> 2. So how should my program works?? I guess my program must try to
> compile Homework.java from each student (guess I can use this by
> executing javac from Runtime). If Homework.class can created, then go
> to step 3. Otherwise, Homework program failed.

This is a simple, simple  script. Heck you could get ANT to go from
directory to directory compiling and then running the tests. Check out
ANT from Apache.

> 3. My project then execute the Homework.class. Well, now
> Homework.class is running. And this is the most confusing part. How
> can i enter first input (for x), second input (for y) and gets the
> output from "calculate" function from the running program?? If this
> part you called as "hijack", how can i interrupt a running program??


So there are a couple of tests types you can do.

The test you seem to be talking about at the moment is a Unit test from
a pre-determined function. Which is pretty simple to do (use ANT and
JUnut for Java (shouldn't take too long).

I also think that you where infering that you should test the overall
input and output of the application. Such as if you made a program that
accepted comands and then responded to them, your program would capture
the standard in and out streams and then read and write to them at will.


>
> Hope someone can help  me, cause this is really important to
> me.........
>

To do the simple version of what you want isn't that hard ... but like
Dave said in an earlier project, to make the real deal is probably out
the realms of any one programmer.

 
Answer #4    Answered By: Midissia Lopez     Answered On: May 23

I had to create  a Java Program just rently that created 5 instances of an object
that was designed to be customer orders. I don't want to give you an answer,
but I will be glad to guide you in a direction that may help.

First create a class  that sets up the actions that you want to perform. Second,
create a second class within the same package that references the first class
and all of the methods objects.

Make the methods in the first class empty constructors.

This is just one direction that you can go.

Have the second class contain the instance of the main method.

With this approach, you will have reusable code ie...The first class can be
referenced by any other class that chooses to do this.

This will get you started. Maybe someone else can add to this.

 
Didn't find what you were looking for? Find more on test a program? Or get search suggestion and latest updates.




Tagged: