diff options
author | Rocky Madden <git@rockymadden.com> | 2012-11-19 12:24:36 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-11-19 12:24:36 -0700 |
commit | e347105de2d80527592533bc060d53de09c7fb78 (patch) | |
tree | 9bf89d840219ca68909f9d6e8520dbb348766c1e /core | |
parent | 58269f6db0268e0ad09d9c1d9cb2174535e20acc (diff) | |
download | stringmetric-e347105de2d80527592533bc060d53de09c7fb78.tar.gz stringmetric-e347105de2d80527592533bc060d53de09c7fb78.tar.bz2 stringmetric-e347105de2d80527592533bc060d53de09c7fb78.zip |
Expanded methods and created spec.
Diffstat (limited to 'core')
-rwxr-xr-x | core/source/core/scala/org/hashtree/stringmetric/phonetic/Alphabet.scala | 19 | ||||
-rwxr-xr-x | core/source/test/scala/org/hashtree/stringmetric/phonetic/AlphabetSpec.scala | 125 |
2 files changed, 143 insertions, 1 deletions
diff --git a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Alphabet.scala b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Alphabet.scala index 172c7b5..fd94e75 100755 --- a/core/source/core/scala/org/hashtree/stringmetric/phonetic/Alphabet.scala +++ b/core/source/core/scala/org/hashtree/stringmetric/phonetic/Alphabet.scala @@ -1,10 +1,27 @@ package org.hashtree.stringmetric.phonetic +import scala.annotation.tailrec + object Alphabet { final val SometimesVowels: Set[Char] = Set('a', 'e', 'i', 'o', 'u', 'y') final val Vowels: Set[Char] = Set('a', 'e', 'i', 'o', 'u') - def is(char: Char) = ((char >= 65 && char <= 90) || (char >= 97 && char <= 122)) + def is(char: Char): Boolean = (char >= 65 && char <= 90) || (char >= 97 && char <= 122) + + def is(charArray: Array[Char]): Boolean = { + @tailrec + def still(ca: Seq[Char]): Boolean = + if (ca.length == 0) true + else + if (!is(ca.head)) false + else still(ca.tail) + + if (charArray.length == 0) false + else if (charArray.length <= 4) still(charArray) + else still(charArray.toList) + } + + def is(string: String): Boolean = is(string.toCharArray) def isSometimesVowel(char: Char): Boolean = (char == 'y' || char == 'Y' || isVowel(char)) diff --git a/core/source/test/scala/org/hashtree/stringmetric/phonetic/AlphabetSpec.scala b/core/source/test/scala/org/hashtree/stringmetric/phonetic/AlphabetSpec.scala new file mode 100755 index 0000000..bfd48c6 --- /dev/null +++ b/core/source/test/scala/org/hashtree/stringmetric/phonetic/AlphabetSpec.scala @@ -0,0 +1,125 @@ +package org.hashtree.stringmetric.phonetic + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class AlphabetSpec extends ScalaTest { + "Alphabet" should provide { + "an overloaded is method which accepts a Char" when passed { + "non-alphabet argument" should returns { + "false" in { + Alphabet.is('0') should be (false) + } + } + "alphabet argument" should returns { + "true" in { + Alphabet.is('a') should be (true) + Alphabet.is('A') should be (true) + } + } + } + "an overloaded is method which accepts an Array[Char]" when passed { + "empty argument" should returns { + "false" in { + Alphabet.is(Array.empty[Char]) should be (false) + } + } + "non-alphabet argument" should returns { + "false" in { + Alphabet.is("hi!".toCharArray) should be (false) + Alphabet.is("helloworld!".toCharArray) should be (false) + } + } + "alphabet argument" should returns { + "true" in { + Alphabet.is("hi".toCharArray) should be (true) + Alphabet.is("helloworld".toCharArray) should be (true) + Alphabet.is("HI".toCharArray) should be (true) + Alphabet.is("HELLOWORLD".toCharArray) should be (true) + } + } + } + "an overloaded is method which accepts a String" when passed { + "empty argument" should returns { + "false" in { + Alphabet.is("") should be (false) + } + } + "non-alphabet argument" should returns { + "false" in { + Alphabet.is("helloworld!") should be (false) + } + } + "alphabet argument" should returns { + "true" in { + Alphabet.is("helloworld") should be (true) + Alphabet.is("HELLOWORLD") should be (true) + } + } + } + "isSometimesVowel method" when passed { + "non-vowel argument" should returns { + "false" in { + Alphabet.isSometimesVowel('z') should be (false) + } + } + "vowel argument" should returns { + "true" in { + Alphabet.isSometimesVowel('a') should be (true) + Alphabet.isSometimesVowel('A') should be (true) + Alphabet.isSometimesVowel('y') should be (true) + Alphabet.isSometimesVowel('Y') should be (true) + } + } + } + "isVowel method" when passed { + "non-vowel argument" should returns { + "false" in { + Alphabet.isVowel('y') should be (false) + } + } + "vowel argument" should returns { + "true" in { + Alphabet.isVowel('a') should be (true) + Alphabet.isVowel('A') should be (true) + } + } + } + "an overloaded startsWith method which accepts an Array[Char]" when passed { + "empty argument" should returns { + "false" in { + Alphabet.startsWith(Array.empty[Char]) should be (false) + } + } + "non-alphabet argument" should returns { + "false" in { + Alphabet.startsWith("1abc".toCharArray) should be (false) + } + } + "alphabet argument" should returns { + "true" in { + Alphabet.startsWith("abc".toCharArray) should be (true) + } + } + } + "an overloaded startsWith method which accepts a String" when passed { + "empty argument" should returns { + "false" in { + Alphabet.startsWith("") should be (false) + } + } + "non-alphabet argument" should returns { + "false" in { + Alphabet.startsWith("1abc") should be (false) + } + } + "alphabet argument" should returns { + "true" in { + Alphabet.startsWith("abc") should be (true) + } + } + } + } +}
\ No newline at end of file |