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

Scala example source code file (t2104.scala)

This example Scala source code file (t2104.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

cs, file, filewriter=, filewriter=, int, int, jfile, jfile, jfile,g, jfile,w, list, list, n=4, test

The Scala t2104.scala source code

/* https://lampsvn.epfl.ch/trac/scala/ticket/2104
   symptom: Source via Buffered Source always loses the last char of the input file.
   cause: BufferedSource? doesn't check return for -1 (EOF), and uses reader.ready() improperly as a substitute.

  test: check over all possible strings of length up to N over alphabet chars:
  write file, then read back its chars, and get back the original.

*/
object Test
{
  val N=4

  import java.io.{ File => JFile }
  import java.io.FileWriter
  import io.Source
  def overwrite(file: JFile,w: FileWriter=>Unit) {
    val fw=new FileWriter(file)
    w(fw)
    fw.close
  }
  def delete_after(f: JFile,g: Source=>Unit) = {
    g(Source.fromFile(f))
    f.delete
  }
  def store_tempfile(f: FileWriter=>Unit)(implicit name:String) : JFile = {
    val tp=JFile.createTempFile(name,null)
    overwrite(tp,f)
    tp
  }

  implicit val name="bug2104"
  val chars=List('\n','\r','a')

  type Cs = List[Char]
  def all_strings(n: Int) : List[Cs] = {
    if (n==0) List(Nil)
    else {
      val sufs=all_strings(n-1)
      chars.flatMap((c)=>sufs.map(c :: _))
    }
  }
  def test(n: Int) {
    for(l <- all_strings(n)) {
      val tmp=store_tempfile((f)=>l.foreach(f.write(_)))
      delete_after(tmp,(s)=>assert(s.toList == l))
    }
  }
  def main(args: Array[String]) {
    (0 until N).foreach(test(_))
  }
}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.