Arrays
Data Types
import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintStream; public class myWorld{ public static OutputStream os = new ByteArrayOutputStream(); public static PrintStream originalOut = System.out; public static PrintStream ps = new PrintStream(os); public static void printArray(int[] arr){ System.setOut(ps); for(int e:arr){ System.out.println(e); } System.setOut(originalOut); } }
- import java.io.ByteArrayOutputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- public class myWorld{
public void printArray(int[] arr){- public static OutputStream os = new ByteArrayOutputStream();
- public static PrintStream originalOut = System.out;
- public static PrintStream ps = new PrintStream(os);
- public static void printArray(int[] arr){
- System.setOut(ps);
- for(int e:arr){
- System.out.println(e);
- }
- System.setOut(originalOut);
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintStream; import java.io.IOException; public class myWorldTest { @Test public void test_myWorldTest() { int[] lst1 = new int[] {12, 15}; myWorld.printArray(lst1); String ls = System.getProperty("line.separator"); assertEquals("12" +ls+ "15" + ls, myWorld.os.toString()); myWorld.os = new ByteArrayOutputStream(); myWorld.ps = new PrintStream(myWorld.os); int[] lst2 = new int[] {12, 15, 123}; myWorld.printArray(lst2); assertEquals("12" +ls+ "15" + ls + "123" + ls, myWorld.os.toString()); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.io.ByteArrayOutputStream;
- import java.io.OutputStream;
- import java.io.PrintStream;
- import java.io.IOException;
public class SolutionTest {public void printArray(int[] arr) {int pass1 = arr[0];int pass2 = arr[1];System.out.println(pass1);System.out.println(pass2);// etc.......//- public class myWorldTest {
- @Test
- public void test_myWorldTest() {
- int[] lst1 = new int[] {12, 15};
- myWorld.printArray(lst1);
- String ls = System.getProperty("line.separator");
- assertEquals("12" +ls+ "15" + ls, myWorld.os.toString());
- myWorld.os = new ByteArrayOutputStream();
- myWorld.ps = new PrintStream(myWorld.os);
- int[] lst2 = new int[] {12, 15, 123};
- myWorld.printArray(lst2);
- assertEquals("12" +ls+ "15" + ls + "123" + ls, myWorld.os.toString());
- }
- }