Skip to content

Instantly share code, notes, and snippets.

@SaiEmaniFlipp
Created November 13, 2019 18:34
Show Gist options
  • Save SaiEmaniFlipp/bfd678d7b95d41b318e839b56b3d0a47 to your computer and use it in GitHub Desktop.
Save SaiEmaniFlipp/bfd678d7b95d41b318e839b56b3d0a47 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static java.util.stream.Collectors.*;
public class DeltaDebug {
/**
* deltaDebug is the method is what will run the delta debug algorithm
* !!!!!! IMPORTANT: DO NOT CHANGE THE TYPE/METHOD SIGNATURE AS IT MUST BE THE SAME AS PROVIDED FOR GRADING !!!!!!
* @param char_granularity - if false, use line granularity for the algorithm
* @param program - the path of the program you're testing
* @param failing_file - path of provided failing input file
* @param error_msg - the program output that Delta should treat as an error
* @param final_minimized_file - path to write minimized output file to
*/
public void deltaDebug(Boolean char_granularity, String program, String failing_file, String error_msg, String final_minimized_file)
{
ArrayList<String> output_list = new ArrayList<>();
/*
TODO: Add your logic for the delta debugging algorithm here
*/
if(char_granularity == false){
output_list = dd_line(program,failing_file,error_msg);
}
// }else if(char_granularity == true){
// output_list = dd_char(failing_file,error_msg);
// }
writeToFile(final_minimized_file, output_list);
}
/**
* readFile reads input from a file line by line
* You can update this method to pass in more parameters or return something if needed
* @param file - file to read
*/
public ArrayList<String> readFile(String file)
{
Scanner scan;
ArrayList<String> file_data = new ArrayList<String>();
try {
scan = new Scanner(new File(file));
while (scan.hasNextLine()){
//do something
file_data.add(scan.next());
}
scan.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return file_data;
}
/**
* writeFile can be used to write to a file
* You can update this method to pass in more/different parameters or return something if needed
* @param file - file to write to
* @param list - this is just a placeholder example, you can use whatever data structure you want
*/
public void writeToFile(String file, ArrayList<String> list)
{
Path out = Paths.get(file);
try {
Files.write(out,list,Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* runCommand can be used to run a program
* You can update this method to pass in more/different parameters or return something if needed
* @param command - complete command you want to run (program location + any command args)
* @param error_msg - error message you're looking for
*/
public boolean runCommand(String command, String error_msg)
{
String s = null;
boolean bug = false;;
try{
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
//do something
if(s == "java.lang.ArrayIndexOutOfBoundsException"){
bug = true;
}else{
bug = false;
}
}
}
catch (IOException e) {
System.out.println(command + "failed to run");
System.exit(-1);
}
return bug;
}
public ArrayList<String> dd_line(String program, String input_file, String error_msg){
ArrayList<String> file_data_read = new ArrayList<String>();
file_data_read = readFile(input_file);
int part_size = 2;
boolean counter;
while(file_data_read.size() >= part_size){
Collection<ArrayList<String>> partionined_data = new ArrayList<>();
partionined_data = partition(file_data_read, part_size);
counter = false;
for(ArrayList<String> part : partionined_data ){
ArrayList<String> file_data_read_copy = new ArrayList<String>();
Collections.copy(file_data_read_copy, file_data_read);
file_data_read_copy.removeAll(part);
String command = "program"+"file_data_read_copy";
if(runCommand(command,error_msg) == true){
file_data_read = file_data_read_copy;
part_size = Math.max(part_size-1, 2);
counter = true;
break;
}
}
if(!counter){
if(part_size == file_data_read.size()){
break;
}
part_size = Math.min(part_size*2, file_data_read.size());
}
}
return file_data_read;
}
public ArrayList<String> dd_char(String input_file, String error_msg){
ArrayList<String> file_data_read = new ArrayList<String>();
file_data_read = readFile(input_file);
return file_data_read;
}
public Collection<ArrayList<String>> partition(ArrayList<String> file_data_read, int part_size){
final AtomicInteger counter = new AtomicInteger();
// final List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7);
// final int chunkSize = 3;
Collection<List<String>> result = file_data_read.stream()
.collect(groupingBy(it -> counter.getAndIncrement() / part_size))
.values();
Collection<ArrayList<String>> arl = new ArrayList<>();
for (List<String> object : result) {
arl.add((ArrayList<String>) object);
}
return arl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment