0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java単体テストライブラリ-Artery/JUnit4-配列の等値判定

Posted at

目次 ⇒ Java単体テストライブラリ-Artery-サンプル

package jp.avaj.lib.test;

import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.util.List;

import org.junit.Test;

import jp.avaj.lib.algo.ArList;

/**
 Java単体テストライブラリ-Artery/JUnit4-配列の等値判定

 Arteryではint[] vs Integer[]やint[] vs long[]などの異なるタイプでも判定することができる

 JUnitでは等値判定はassertArrayEqualsを使用する(assertEqualsではない)
 JUnitではint[] vs Integer[]やint[] vs long[]など異なるタイプでは判定できない
 */
public class Q02_01 {
  //////// 以下はArteryのサンプル
  public static void main(String[] args) {
    // テストケースの開始を宣言する ⇒ 集計が不要な場合は不要
    ArTest.startTestCase("Q02_01");

    // int[] vs int[]
    {
      int[] value0 = new int[]{0,1,2};
      int[] value1 = new int[]{0,1,2};
      ArTest.equals("int[] vs int[]","value0",value0,"value1",value1);
      value1 = new int[]{10,11,12};
      ArTest.equals("int[] vs int[] (NG)","value0",value0,"value1",value1);
    }
    // int[] vs Integer[]
    {
      int[] value0 = new int[]{0,1,2};
      Integer[] value1 = new Integer[]{0,1,2};
      ArTest.equals("int[] vs Integer[]","value0",value0,"value1",value1);
      value1 = new Integer[]{10,11,12};
      ArTest.equals("int[] vs Integer[] (NG)","value0",value0,"value1",value1);
    }
    // int[] vs Long[]
    {
      int[] value0 = new int[]{0,1,2};
      Long[] value1 = new Long[]{0L,1L,2L};
      ArTest.equals("int[] vs Long[]","value0",value0,"value1",value1);
      value1 = new Long[]{10L,11L,12L};
      ArTest.equals("int[] vs Long[] (NG)","value0",value0,"value1",value1);
    }
    // int[] vs Double[]
    {
      int[] value0 = new int[]{0,1,2};
      Double[] value1 = new Double[]{0D,1D,2D};
      ArTest.equals("int[] vs Double[]","value0",value0,"value1",value1);
      value1 = new Double[]{10D,11D,12D};
      ArTest.equals("int[] vs Double[] (NG)","value0",value0,"value1",value1);
    }
    // int[] vs BigDecimal[]
    {
      int[] value0 = new int[]{0,1,2};
      BigDecimal[] value1 = new BigDecimal[]{new BigDecimal(0),new BigDecimal(1),new BigDecimal(2)};
      ArTest.equals("int[] vs BigDecimal[]","value0",value0,"value1",value1);
      value1 = new BigDecimal[]{new BigDecimal(10),new BigDecimal(11),new BigDecimal(12)};
      ArTest.equals("int[] vs BigDecimal[] (NG)","value0",value0,"value1",value1);
    }
    // int[] vs List<Integer>
    {
      int[] value0 = new int[]{0,1,2};
      List<Integer> value1 = ArList.construct(new Integer[]{0,1,2});
      ArTest.equals("int[] vs List<Integer>","value0",value0,"value1",value1);
      value1 = ArList.construct(new Integer[]{10,11,12});
      ArTest.equals("int[] vs List<Integer> (NG)","value0",value0,"value1",value1);
    }
    // int[] vs String[] ⇒ 判定できるけど、使わない方がよいかも?
    {
      int[] value0 = new int[]{0,1,2};
      String[] value1 = new String[]{"0","1","2"};
      ArTest.equals("int[] vs String[]","value0",value0,"value1",value1);
      value1 = new String[]{"10","11","12"};
      ArTest.equals("int[] vs String[] (NG)","value0",value0,"value1",value1);
    }
    // String[] vs String[]
    {
      String[] value0 = new String[]{"0","1","2"};
      String[] value1 = new String[]{"0","1","2"};
      ArTest.equals("String[] vs String[]","value0",value0,"value1",value1);
      value1 = new String[]{"10","11","12"};
      ArTest.equals("String[] vs String[] (NG)","value0",value0,"value1",value1);
    }
    // String[] vs List<String>[]
    {
      String[] value0 = new String[]{"0","1","2"};
      List<String> value1 = ArList.construct(new String[]{"0","1","2"});
      ArTest.equals("String[] vs List<String>[]","value0",value0,"value1",value1);
      value1 = ArList.construct(new String[]{"10","11","12"});
      ArTest.equals("String[] vs List<String>[] (NG)","value0",value0,"value1",value1);
    }

    // テストケースを終了する ⇒ 集計が不要な場合は不要
    ArTest.endTestCase();
  }

  //////// 以下はJUnit4のサンプル
  // int[] vs int[] - 等しい場合 ⇒ しかしNGになる
  // 「assertArrayEqualsを使う」ということを知らない場合は間違うかも..
  @Test
  public void test00() {
    assertEquals(new int[]{0,1,2},new int[]{0,1,2});
  }

  // int[] vs int[] - 等しい場合 ⇒ これでOK
  @Test
  public void test01() {
    assertArrayEquals(new int[]{0,1,2},new int[]{0,1,2});
  }

  // int[] vs Integer[] - 等しい場合 ⇒ これはコンパイルエラー
  // このくらいはやってほしいけど...
  @Test
  public void test02() {
//    assertArrayEquals(new int[]{0,1,2},new Integer[]{0,1,2});
  }

  // int[] vs long[] - 等しい場合 ⇒ これもコンパイルエラー
  // このくらいはやってほしいけど...
  @Test
  public void test03() {
//    assertArrayEquals(new int[]{0,1,2},new long[]{0,1,2});
  }

  // double[] vs double[],判定誤差を指定する必要がある ⇒ 等しい場合
  @Test
  public void test04() {
    assertArrayEquals(new double[]{0D,1D,2D},new double[]{0D,1D,2D},0.5D);
  }

  // double[] vs double[],判定誤差を指定する必要がある ⇒ 等しくない場合
  @Test
  public void test05() {
    assertArrayEquals(new double[]{0D,1D,2D},new double[]{10D,11D,12D},0.5D);
  }


  // String[] va String[] - 等しい場合
  @Test
  public void test06() {
    assertArrayEquals(new String[]{"a","b","c"},new String[]{"a","b","c"});
  }
  // String[] va String[] - 等しくない場合
  @Test
  public void test07() {
    assertArrayEquals(new String[]{"a","b","c"},new String[]{"x","y","z"});
  }
}

結果は次の通り
無題.png

result.txt
**** Q02_01 start ****
OK int[] vs int[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs int[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:32)
OK int[] vs Integer[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Integer[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:40)
OK int[] vs Long[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Long[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:48)
OK int[] vs Double[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs Double[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:56)
OK int[] vs BigDecimal[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs BigDecimal[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:64)
OK int[] vs List<Integer>:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs List<Integer> (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:72)
OK int[] vs String[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG int[] vs String[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:80)
OK String[] vs String[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG String[] vs String[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:88)
OK String[] vs List<String>[]:value0=[0, 1, 2]:value1=[0, 1, 2]
NG String[] vs List<String>[] (NG):value0=[0, 1, 2]:value1=[10, 11, 12]
jp.avaj.lib.test.Q02_01.main(Q02_01.java:96)
**** Q02_01 summary ****
test count = 18
success    = 9

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?