-
勝手に決めた仕様
- Intの最大値、最小値まで
-
文字列としても表現できる
- [x]例: 下端点 3, 上端点 8 の整数閉区間の文字列表記は "[3,8]"
- -10, -5 は作成できる
- 0, -1は 例外が得られる
-
上端点より下端点が大きい閉区間を作ることはできない。
- 下端点と上端点をもつ
- 下端点と上端点をもつ
- コンストラクタ
- 上端と下端が同じだったらOK
- 上端より下端のパラメータが大きかったらバリデーションエラーを発生させる(IllegalArgmentException)
-
整数の閉区間は指定した整数を含むかどうかを判定できる。contains
- [3, 8]に対して、3, 6, 8は含む
- 2, 9は含まない
-
別の閉区間と等価かどうか判定できる。 equivalent
- [4, 9] に対して [4,9]は等価
- [4,10]は等価でない
- [3,9]は等価でない
- [5,8]は等価でない
Last active
August 3, 2017 02:55
-
-
Save cm-wada-yusuke/14cd4778ed64cdf67e104b7412bcf937 to your computer and use it in GitHub Desktop.
2017年8月1日(火)TDDワークショップ Scala チーム成果物 課題はこれ:http://bit.ly/2uEATGA ブログはこれ:http://dev.classmethod.jp/event/twada-tdd-workshop-sapporo-report/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tdd | |
class ClosedRange(val lowerEndpoint: Int, val upperEndpoint: Int) { | |
if (lowerEndpoint > upperEndpoint) { | |
throw new IllegalArgumentException | |
} | |
override def toString: String = s"[$lowerEndpoint,$upperEndpoint]" | |
def contains(num: Int): Boolean = lowerEndpoint <= num && num <= upperEndpoint | |
def equivalent(other: ClosedRange): Boolean = | |
this.lowerEndpoint == other.lowerEndpoint && | |
this.upperEndpoint == other.upperEndpoint | |
def includes(other: ClosedRange): Boolean = | |
this.lowerEndpoint <= other.lowerEndpoint && | |
other.upperEndpoint <= this.upperEndpoint | |
// 最初これを書いてましたがテスト記載後最新の記載にリファクタしました | |
// (this.lowerEndpoint <= other.lowerEndpoint && other.lowerEndpoint <= this.upperEndpoint) && | |
// (this.lowerEndpoint <= other.upperEndpoint && other.upperEndpoint <= this.upperEndpoint) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tdd | |
import org.scalatest.{ MustMatchers, WordSpec } | |
class ClosedRangeSpec extends WordSpec with MustMatchers { | |
"閉区間の文字列での出力" should { | |
"1と3を渡したら[1,3]を得られる" in { | |
val closedRange = new ClosedRange(1, 3) | |
closedRange.toString mustEqual "[1,3]" | |
} | |
"1と1を渡したら[1,1]が得られる" in { | |
val closedRange = new ClosedRange(1, 1) | |
closedRange.toString mustEqual "[1,1]" | |
} | |
"3,1を渡したらIllegalArgumentExceptionが発生する" in { | |
an[IllegalArgumentException] must be thrownBy new ClosedRange(3, 1) | |
} | |
"-10と-5を渡したら[-10,-5]が得られる" in { | |
val closedRange = new ClosedRange(-10, -5) | |
closedRange.toString mustEqual "[-10,-5]" | |
} | |
"0と-1を渡したらIllegalArgumentExceptionが発生する" in { | |
an[IllegalArgumentException] must be thrownBy new ClosedRange(0, -1) | |
} | |
} | |
"指定した整数を含むかどうか判定" should { | |
trait CommonContext { | |
val sut = new ClosedRange(3, 8) | |
} | |
"[3,8]の閉区間オブジェクトに3を指定した場合trueが得られる" in new CommonContext { | |
sut.contains(3) mustBe true | |
} | |
"[3,8]の閉区間オブジェクトに6を指定した場合trueが得られる" in new CommonContext { | |
sut.contains(6) mustBe true | |
} | |
"[3,8]の閉区間オブジェクトに8を指定した場合trueが得られる" in new CommonContext { | |
sut.contains(8) mustBe true | |
} | |
"[3,8]の閉区間オブジェクトに2を指定した場合falseが得られる" in new CommonContext { | |
sut.contains(2) mustBe false | |
} | |
"[3,8]の閉区間オブジェクトに9を指定した場合falseが得られる" in new CommonContext { | |
sut.contains(9) mustBe false | |
} | |
} | |
"別の閉区間と等価かどうか判定" should { | |
trait CommonContext { | |
val sut = new ClosedRange(4, 9) | |
} | |
"[4,9]の閉区間オブジェクトと[4,9]の閉区間オブジェクトが等価であること" in new CommonContext { | |
val other = new ClosedRange(4, 9) | |
sut.equivalent(other) mustBe true | |
} | |
"[4,9]の閉区間オブジェクトと[4,10]の閉区間オブジェクトが等価でないこと" in new CommonContext { | |
val other = new ClosedRange(4, 10) | |
sut.equivalent(other) mustBe false | |
} | |
"[4,9]の閉区間オブジェクトと[3,9]の閉区間オブジェクトが等価でないこと" in new CommonContext { | |
val other = new ClosedRange(3, 9) | |
sut.equivalent(other) mustBe false | |
} | |
"[4,9]の閉区間オブジェクトと[5,8]の閉区間オブジェクトが等価でないこと" in new CommonContext { | |
val other = new ClosedRange(5, 8) | |
sut.equivalent(other) mustBe false | |
} | |
} | |
"別の閉区間を含むかどうか判定" should { | |
trait CommonContext { | |
val sut = new ClosedRange(-100, 100) | |
} | |
"[-100, 100]の閉区間オブジェクトは[-100,100]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(-100, 100) | |
sut.includes(other) mustBe true | |
} | |
"[-100, 100]の閉区間オブジェクトは[-99,99]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(-99, 99) | |
sut.includes(other) mustBe true | |
} | |
"[-100, 100]の閉区間オブジェクトは[-101,100]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(-101, 100) | |
sut.includes(other) mustBe false | |
} | |
"[-100, 100]の閉区間オブジェクトは[-100,101]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(-100, 101) | |
sut.includes(other) mustBe false | |
} | |
"[-100, 100]の閉区間オブジェクトは[-102,-101]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(-102, -101) | |
sut.includes(other) mustBe false | |
} | |
"[-100, 100]の閉区間オブジェクトは[101,102]の閉区間オブジェクトを含む" in new CommonContext { | |
val other = new ClosedRange(101, 102) | |
sut.includes(other) mustBe false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment