Created
January 27, 2011 12:42
-
-
Save jpcaruana/798457 to your computer and use it in GitHub Desktop.
Rationnel.scala
This file contains hidden or 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
object Rationnel { | |
implicit def conversionIntVersRationnel(i :Int) :Rationnel = Rationnel(i) | |
def apply(a :Int) :Rationnel = new Rationnel(a,1) | |
def pgcd(a :Int, b :Int) :Int = { | |
if (b == 0) a | |
else pgcd(b, a%b) | |
} | |
} | |
case class Rationnel(n :Int, d :Int) { | |
require(d != 0) | |
private val pgcd = Rationnel.pgcd(n, d) | |
val numerateur = n/pgcd | |
val denominateur = d/pgcd | |
def + (autre :Rationnel) :Rationnel = | |
Rationnel(numerateur * autre.denominateur + autre.numerateur * denominateur, denominateur*autre.denominateur) | |
def + (autre :Int) :Rationnel = Rationnel(autre) + this | |
override def toString = numerateur + "/" + denominateur | |
override def equals(autre :Any) = autre match { | |
case ratio :Rationnel => numerateur == ratio.numerateur && denominateur == ratio.denominateur | |
case _ => false | |
} | |
override def hashCode = 41 * (numerateur + 41) + denominateur | |
} |
This file contains hidden or 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
import org.scalatest.FunSuite | |
class RationnelTest extends FunSuite { | |
test("creation de 1/2") { | |
val unDemi = Rationnel(1, 2) | |
assert(unDemi.numerateur === 1) | |
assert(unDemi.denominateur === 2) | |
} | |
test("toString renvoie n/d") { | |
assert(Rationnel(1,2).toString === "1/2") | |
} | |
test("pgcd(0,0) = 0") { | |
assert(Rationnel.pgcd(0, 0) === 0) | |
} | |
test("pgcd(1,0) = 1") { | |
assertPgcd(1, 0) est 1 | |
} | |
test("pgcd(6,3) = 3") { | |
assertPgcd(6, 3) est 3 | |
} | |
test("pgcd(9,6) = 3") { | |
assertPgcd(9, 6) est 3 | |
} | |
test("2/4 = 1/2") { | |
assert(Rationnel(1,2) === Rationnel(2,4)) | |
assert(Rationnel(1,2).hashCode === Rationnel(2,4).hashCode) | |
} | |
test("Rationnel(2) = Rationnel (2,1)") { | |
assert(Rationnel(2) === Rationnel (2,1)) | |
} | |
test("Rationnel(1,0) envoie IllegalArgumentException") { | |
intercept[IllegalArgumentException] { | |
Rationnel(1,0) | |
} | |
} | |
test("1/2 + 1/3 = 5/6") { | |
assert(Rationnel(5,6) === Rationnel(1,2) + Rationnel(1,3)) | |
} | |
test("1/2 + 1 = 3/2") { | |
assert(Rationnel(3,2) === Rationnel(1,2) + 1) | |
} | |
test("1 + 1/2 = 3/2") { | |
import Rationnel.conversionIntVersRationnel | |
assert(Rationnel(3,2) === 1 + Rationnel(1,2)) | |
} | |
class AssertPgcd (val a :Int, val b:Int) { | |
def est(pgcdAttendu :Int) = { | |
assert(Rationnel.pgcd(a, b) === pgcdAttendu) | |
assert(Rationnel.pgcd(b, a) === pgcdAttendu) | |
} | |
} | |
private def assertPgcd(a :Int, b: Int) = { | |
new AssertPgcd(a, b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment