Advertisement
Rementai

tijo1

Feb 29th, 2024
1,105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. //Main.java
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         assertAddMethod();
  5.         MaxFinderTest.testMaxFinder();
  6.     }
  7.  
  8.     static void assertAddMethod() {
  9.         int result = add(1, 2);
  10.         assert result == 3 : "Given 1 and 2, When add method is called, Then it should return 3";
  11.         System.out.println("Add method test passed");
  12.     }
  13.  
  14.     static int add(int first, int second) {
  15.         return first + second;
  16.     }
  17. }
  18.  
  19.  
  20. //MaxFinder.java
  21. public class MaxFinder {
  22.     public static Integer max(Integer[] digits) {
  23.         if (digits == null || digits.length == 0) {
  24.             return null;
  25.         }
  26.  
  27.         Integer max = digits[0];
  28.         for (int i = 1; i < digits.length; i++) {
  29.             if (digits[i] != null && (max == null || digits[i] > max)) {
  30.                 max = digits[i];
  31.             }
  32.         }
  33.  
  34.         return max;
  35.     }
  36. }
  37.  
  38.  
  39. //MaxFinderTest.java
  40. public class MaxFinderTest {
  41.     public static void testMaxFinder() {
  42.         testNullInput();
  43.         testEmptyArray();
  44.         testSingleElementArray();
  45.         testMultipleElementsArray();
  46.         System.out.println("All tests passed successfully.");
  47.     }
  48.  
  49.     private static void testNullInput() {
  50.         Integer[] digits = null;
  51.         assert MaxFinder.max(digits) == null : "Given null input, When max method is called, Then it should return null";
  52.         System.out.println("Test for null input passed");
  53.     }
  54.  
  55.     private static void testEmptyArray() {
  56.         Integer[] digits = {};
  57.         assert MaxFinder.max(digits) == null : "Given empty array, When max method is called, Then it should return null";
  58.         System.out.println("Test for empty array passed");
  59.     }
  60.  
  61.     private static void testSingleElementArray() {
  62.         Integer[] digits = {5};
  63.         assert MaxFinder.max(digits) == 5 : "Given single element array, When max method is called, Then it should return the element itself";
  64.         System.out.println("Test for single element array passed");
  65.     }
  66.  
  67.     private static void testMultipleElementsArray() {
  68.         Integer[] digits = {5, 3, 9, 1, 7};
  69.         assert MaxFinder.max(digits) == 9 : "Given multiple elements array, When max method is called, Then it should return the maximum element";
  70.         System.out.println("Test for multiple elements array passed");
  71.     }
  72. }
  73.  
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement