210805 목 TIL

✔ Learned

Arrays.copyOf

public void push(int data) {
        if(size == 0) {
            stackArray = new int[]{data};
            size++;
            return;
        }

        stackArray = Arrays.copyOf(stackArray, size+1);
        stackArray[size] = data;
        size++;
    }
    public void pop() {
        if(size==0) {
            throw new EmptyStackException();
        }
        stackArray = Arrays.copyOfRange(stackArray, 0, size-1);
        size--;
    }
int[] array = {23, 43, 55, 12, 65, 88, 92};

int[] copiedArray = Arrays.copyOfRange(array, 1, 4);

assertTrue(3 == copiedArray.length);
assertTrue(copiedArray[0] == array[1]);
assertTrue(copiedArray[1] == array[2]);
assertTrue(copiedArray[2] == array[3]);

print test 하는 방법

  • println 테스트 하는 방법
  • https://www.baeldung.com/java-testing-system-out-println
    private final PrintStream standardOut = System.out;
    private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
    @BeforeEach
    public void setUp() {
        System.setOut(new PrintStream(outputStreamCaptor));
    }
@Test
void givenSystemOutRedirection_whenInvokePrintln_thenOutputCaptorSuccess() {
    print("Hello Baeldung Readers!!");        
    Assert.assertEquals("Hello Baeldung Readers!!", outputStreamCaptor.toString()
      .trim());
}
@AfterEach
public void tearDown() {
    System.setOut(standardOut);
}

좋은 웹페이지 즐겨찾기