Aldin_SXR

ShellSortTest

Jun 22nd, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package ds.shell;
  2.  
  3. import static org.junit.jupiter.api.Assertions.*;
  4.  
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. import org.junit.jupiter.api.AfterAll;
  10. import org.junit.jupiter.api.BeforeAll;
  11. import org.junit.jupiter.api.Test;
  12.  
  13. class ShellSortTest {
  14.     static Student[] students;
  15.     static String originalPath = "/path/to/students.csv";
  16.     static String sortedPath = "/path/to/sorted_students.csv";
  17.  
  18.     @BeforeAll
  19.     static void setUpBeforeClass() throws Exception {
  20.         students = TextFileParser.readFile(originalPath);
  21.     }
  22.  
  23.     @AfterAll
  24.     static void tearDownAfterClass() throws Exception {
  25.         students = null;
  26.     }
  27.  
  28.     @Test
  29.     void testReadsFromFile() throws IOException {
  30.         assertNotNull(students);
  31.         assertTrue(students[0] instanceof Student);
  32.         assertEquals(1000000, students.length);
  33.     }
  34.    
  35.     @Test
  36.     void testSortsStudents() {
  37.         ShellSort.sort(students);
  38.         assertEquals(100, students[0].getStudentID());
  39.         assertEquals(101, students[1].getStudentID());
  40.         assertEquals(102, students[2].getStudentID());
  41.         assertEquals(20110396, students[543823].getStudentID());
  42.         assertEquals(10185139, students[385139].getStudentID());
  43.         assertEquals(30333094, students[999998].getStudentID());
  44.         assertEquals(30333095, students[999999].getStudentID());
  45.     }
  46.    
  47.     @Test
  48.     void testWritesToFile() throws IOException {
  49.         new File(sortedPath).delete(); // delete old file
  50.        
  51.         ShellSort.sort(students);
  52.         TextFileParser.writeFile(students, sortedPath);
  53.         assertTrue(new File(sortedPath).exists());
  54.  
  55.         Scanner scanner = new Scanner(new File(sortedPath));
  56.        
  57.         /* Check if students are loaded correctly from file lines */
  58.         String line = scanner.nextLine();
  59.         String[] parts = line.split(";");
  60.         Student s1 = new Student(Integer.parseInt(parts[0]), parts[1], parts[2],
  61.                                  parts[3], parts[4], Integer.parseInt(parts[5]));
  62.         assertEquals(100, s1.getStudentID());      
  63.                
  64.         scanner.close();
  65.     }
  66.    
  67.     @Test
  68.     void testStudentsAreComparable() {
  69.         Student s1 = new Student(1, "A", "01-01-1997", "IBU", "IT", 2018);
  70.         Student s2 = new Student(2, "B", "31-12-1997", "IBU", "IT", 2018);
  71.  
  72.         assertTrue(s2.compareTo(s1) > 0);
  73.         assertTrue(s1.compareTo(s2) < 0);
  74.         assertTrue(s1.compareTo(s1) == 0);
  75.     }
  76.  
  77. }
Add Comment
Please, Sign In to add comment