alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (primitive-eqeq.scala)

This example Scala source code file (primitive-eqeq.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

any, any, anyval, anyval, bigdecimal, bigint, char, double, properties, short, short, test, test

The Scala primitive-eqeq.scala source code

import org.scalacheck._
import Prop._
import Gen._

object Test extends Properties("==") {
  def equalObjectsEqualHashcodes(x: Any, y: Any) = (x != y) || (x == y && x.## == y.##)
  
  // ticket #2087
  property("short/char") = forAll { (x: Short) => {
      val ch: Char = x.toChar
      (x == ch) == (ch == x)
    }
  }

  property("symmetry") = forAll { (x: AnyVal, y: AnyVal) => (x == y) == (y == x) }
  property("transitivity") = forAll { (x: AnyVal, y: AnyVal, z: AnyVal) => x != y || y != z || x == z }
  
  property("##") = forAll { 
    (x: Short) => {
      val anyvals = List(x.toByte, x.toChar, x, x.toInt, x.toLong, x.toFloat, x.toDouble, BigInt(x), BigDecimal(x))
      val shortAndLarger = anyvals drop 2

      val result = (
        ((anyvals, anyvals).zipped forall equalObjectsEqualHashcodes) && 
        ((shortAndLarger, shortAndLarger).zipped forall (_ == _)) && 
        ((shortAndLarger, shortAndLarger).zipped forall ((x, y) => (x: Any) == (y: Any)))
      )
      result
    }
  }
  property("## 2") = forAll {
    (dv: Double) => {
      val fv = dv.toFloat
      (fv != dv) || (fv.## == dv.##)
    }
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala primitive-eqeq.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.