lib: add some examples/unit tests
authorJean Privat <jean@pryen.org>
Sat, 17 Aug 2013 20:32:14 +0000 (16:32 -0400)
committerJean Privat <jean@pryen.org>
Sat, 17 Aug 2013 20:32:14 +0000 (16:32 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/collection/abstract_collection.nit
lib/standard/kernel.nit
lib/standard/string.nit

index cc58acd..852d6a2 100644 (file)
@@ -54,25 +54,48 @@ interface Collection[E]
        end
 
        # Is there no item in the collection?
+       #
+       #     assert [1,2,3].is_empty  == false
+       #     assert [1..1[.is_empty   == true
        fun is_empty: Bool is abstract 
 
        # Number of items in the collection.
+       #
+       #     assert [10,20,30].length == 3
+       #     assert [20..30[.length   == 10
        fun length: Int is abstract
 
        # Is `item` in the collection ?
        # Comparisons are done with ==
+       #
+       #     assert [1,2,3].has(2)    == true
+       #     assert [1,2,3].has(9)    == false
+       #     assert [1..5[.has(2)     == true
+       #     assert [1..5[.has(9)     == false
        fun has(item: E): Bool is abstract
 
        # Is the collection contain only `item`?
        # Comparisons are done with ==
        # Return true if the collection is empty.
+       #
+       #     assert [1,1,1].has_only(1)         == true
+       #     assert [1,2,3].has_only(1)         == false
+       #     assert [1..1].has_only(1)          == true
+       #     assert [1..3].has_only(1)          == false
+       #     assert [3..3[.has_only(1)          == true # empty collection
+       #
+       # ENSURE `is_empty implies (return) == true`
        fun has_only(item: E): Bool is abstract
 
        # How many occurrences of `item` are in the collection?
        # Comparisons are done with ==
+       #
+       #    assert [10,20,10].count(10)         == 2
        fun count(item: E): Int is abstract
 
        # Return one the item of the collection
+       #
+       #    assert [1,2,3].first                == 1
        fun first: E is abstract
 end
 
index ac3d5b6..efb6819 100644 (file)
@@ -216,8 +216,14 @@ universal Float
        fun -(i: Float): Float is intern
        fun *(i: Float): Float is intern
        fun /(i: Float): Float is intern
-       
+
        # The integer part of `self`.
+       #
+       #     assert (0.0).to_i      == 0
+       #     assert (0.9).to_i      == 0
+       #     assert (-0.9).to_i     == 0
+       #     assert (9.9).to_i      == 9
+       #     assert (-9.9).to_i     == -9
        fun to_i: Int is intern
 end
 
@@ -241,11 +247,22 @@ universal Int
        redef fun -(i) is intern
        fun *(i: Int): Int is intern
        fun /(i: Int): Int is intern
-       fun %(i: Int): Int is intern   
+       fun %(i: Int): Int is intern
+
+       # `i` bits shift fo the left (aka <<)
+       #
+       #     assert 5.lshift(1)    == 10
        fun lshift(i: Int): Int is intern
-       fun rshift(i: Int): Int is intern   
+
+       # `i` bits shift fo the right (aka >>)
+       #
+       #     assert 5.rshift(1)    == 2
+       fun rshift(i: Int): Int is intern
 
        # The float equivalent of `self`
+       #
+       #     assert 5.to_f         == 5.0
+       #     assert 5.to_f         != 5 # Float and Int are not equals
        fun to_f: Float is intern
 
        redef fun succ is intern
@@ -259,7 +276,7 @@ universal Int
                        return -d
                end
        end
-       
+
        redef fun <=>(other)
        do
                if self < other then
@@ -299,9 +316,15 @@ universal Int
        end
 
        # The character whose ASCII value is `self`.
+       #
+       #      assert 65.ascii   == 'A'
+       #      assert 10.ascii   == '\n'
        fun ascii: Char is intern
 
        # Number of digits of an integer in base `b` (plus one if negative)
+       #
+       #     assert 123.digit_count(10) == 3
+       #     assert 123.digit_count(2) == 7 # 1111011 in binary
        fun digit_count(b: Int): Int
        do
                if b == 10 then return digit_count_base_10
@@ -349,7 +372,9 @@ universal Int
 
        # Return the corresponding digit character
        # If 0 <= `self` <= 9, return the corresponding character.
+       #     assert 5.to_c    == '5'
        # If 10 <= `self` <= 36, return the corresponding letter [a..z].
+       #     assert 15.to_c   == 'f'
        fun to_c: Char
        do
                assert self >= 0 and self <= 36 # TODO plan for this
@@ -383,6 +408,10 @@ universal Int
        end
 
        # The absolute value of self
+       #
+       #     assert (-10).abs   == 10
+       #     assert 10.abs    == 10
+       #     assert 0.abs     == 0
        fun abs: Int
        do
            if self >= 0
@@ -425,6 +454,8 @@ universal Char
        end
 
        # If `self` is a digit then return this digit else return -1.
+       #
+       #     assert '5'.to_i    == 5
        fun to_i: Int
        do
 
@@ -438,6 +469,9 @@ universal Char
        end
 
        # the ascii value of self
+       #
+       #     assert 'a'.ascii    == 97
+       #     assert '\n'.ascii   == 10
        fun ascii: Int is intern
 
        redef fun +(i) is intern
@@ -445,6 +479,10 @@ universal Char
 
        # Return the lower case version of self.
        # If self is not a letter, then return self
+       #
+       #     assert 'A'.to_lower  == 'a'
+       #     assert 'a'.to_lower  == 'a'
+       #     assert '$'.to_lower  == '$'
        fun to_lower: Char
        do
                if is_upper then
@@ -456,6 +494,10 @@ universal Char
 
        # Return the upper case version of self.
        # If self is not a letter, then return self
+       #
+       #     assert 'a'.to_upper  == 'A'
+       #     assert 'A'.to_upper  == 'A'
+       #     assert '$'.to_upper  == '$'
        fun to_upper: Char
        do
                if is_lower then
@@ -464,26 +506,45 @@ universal Char
                        return self
                end
        end
-       
+
        # Is self a digit? (from '0' to '9')
+       #
+       #     assert '0'.is_digit   == true
+       #     assert '9'.is_digit   == true
+       #     assert 'a'.is_digit   == false
        fun is_digit : Bool
        do
                return self >= '0' and self <= '9'
        end
-       
+
        # Is self a lower case letter? (from 'a' to 'z')
+       #
+       #     assert 'a'.is_lower   == true
+       #     assert 'z'.is_lower   == true
+       #     assert 'A'.is_lower   == false
+       #     assert '$'.is_lower   == false
        fun is_lower : Bool
        do
                return self >= 'a' and self <= 'z'
        end
-       
+
        # Is self a upper case letter? (from 'A' to 'Z')
+       #
+       #     assert 'A'.is_upper   == true
+       #     assert 'A'.is_upper   == true
+       #     assert 'z'.is_upper   == false
+       #     assert '$'.is_upper   == false
        fun is_upper : Bool
        do
                return self >= 'A' and self <= 'Z'
        end
-       
+
        # Is self a letter? (from 'A' to 'Z' and 'a' to 'z')
+       #
+       #     assert 'A'.is_letter  == true
+       #     assert 'A'.is_letter  == true
+       #     assert 'z'.is_letter  == true
+       #     assert '$'.is_letter  == false
        fun is_letter : Bool
        do
                return is_lower or is_upper
index b136c58..bf569c2 100644 (file)
@@ -32,6 +32,8 @@ abstract class AbstractString
        readable private var _items: NativeString
 
        # Access a character at `index` in the string.
+       #
+       #     assert "abcd"[2]         == 'c'
        redef fun [](index) do return _items[index]
 
        # Create a substring.
@@ -109,6 +111,9 @@ abstract class AbstractString
        fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
 
        # If `self` contains only digits, return the corresponding integer
+       #
+       #     assert "123".to_i        == 123
+       #     assert "-1".to_i         == -1
        fun to_i: Int
        do
                # Shortcut
@@ -116,6 +121,10 @@ abstract class AbstractString
        end
 
        # If `self` contains a float, return the corresponding float
+       #
+       #     assert "123".to_f        == 123.0
+       #     assert "-1".to_f         == -1.0
+       #     assert "-1.2e-3".to_f    == -1.2e-3
        fun to_f: Float
        do
                # Shortcut
@@ -126,6 +135,8 @@ abstract class AbstractString
        fun to_hex: Int do return a_to(16)
 
        # If `self` contains only digits and letters, return the corresponding integer in a given base
+       #
+       #     assert "120".a_to(3)     == 15
        fun a_to(base: Int) : Int
        do
                var i = 0
@@ -154,6 +165,11 @@ abstract class AbstractString
        end
 
        # Returns `true` if the string contains only Numeric values (and one "," or one "." character)
+       #
+       #     assert "123".is_numeric  == true
+       #     assert "1.2".is_numeric  == true
+       #     assert "1,2".is_numeric  == true
+       #     assert "1..2".is_numeric == false
        fun is_numeric: Bool
        do
                var has_point_or_comma = false
@@ -173,6 +189,8 @@ abstract class AbstractString
        end
 
        # A upper case version of `self`
+       #
+       #     assert "Hello World!".to_upper     == "HELLO WORLD!"
        fun to_upper: String
        do
                var s = new Buffer.with_capacity(length)
@@ -181,6 +199,8 @@ abstract class AbstractString
        end
 
        # A lower case version of `self`
+       #
+       #     assert "Hello World!".to_lower     == "hello world!"
        fun to_lower : String
        do
                var s = new Buffer.with_capacity(length)
@@ -190,6 +210,9 @@ abstract class AbstractString
 
        # Trims trailing and preceding white spaces
        # A whitespace is defined as any character which ascii value is less than or equal to 32
+       #
+       #     assert "  Hello  World !  ".trim   == "Hello  World !"
+       #     assert "\na\nb\tc\t".trim          == "a\nb\tc"
        fun trim: String
        do
                if self._length == 0 then return self.to_s
@@ -489,6 +512,8 @@ class String
        end
 
        # The concatenation of `self` with `s`
+       #
+       #     assert "hello " + "world!"         == "hello world!"
        fun +(s: String): String
        do
                var my_length = self._length
@@ -505,6 +530,10 @@ class String
        end
 
        # `i` repetitions of `self`
+       #
+       #     assert "abc"*3           == "abcabcabc"
+       #     assert "abc"*1           == "abc"
+       #     assert "abc"*0           == ""
        fun *(i: Int): String
        do
                assert i >= 0
@@ -688,6 +717,8 @@ redef class Object
        private fun native_class_name: NativeString is intern
 
        # The class name of the object.
+       #
+       #    assert 5.class_name == "Int"
        fun class_name: String do return new String.from_cstring(native_class_name)
 
        # Developer readable representation of `self`.
@@ -711,6 +742,8 @@ redef class Object
 end
 
 redef class Bool
+       #     assert true.to_s         == "true"
+       #     assert false.to_s        == "false"
        redef fun to_s
        do 
                if self then 
@@ -750,6 +783,9 @@ redef class Int
        private fun native_int_to_s(len: Int): NativeString is extern "native_int_to_s"
 
        # return displayable int in base 10 and signed
+       #
+       #     assert 1.to_s            == "1"
+       #     assert (-123).to_s       == "-123"
        redef fun to_s do
                var len = digit_count(10)
                return new String.from_cstring(native_int_to_s(len))
@@ -769,7 +805,7 @@ redef class Int
 end
 
 redef class Float
-       # Pretty print self, print needed decimals up to a max of 6.
+       # Pretty print self, print needoed decimals up to a max of 3.
        redef fun to_s do
                var str = to_precision( 3 )
                var len = str.length
@@ -824,6 +860,7 @@ redef class Float
 end
 
 redef class Char
+       #     assert 'x'.to_s    == "x"
        redef fun to_s
        do
                var s = new Buffer.with_capacity(1)
@@ -866,6 +903,9 @@ redef class Collection[E]
        end
 
        # Concatenate and separate each elements with `sep`.
+       #
+       #     assert [1, 2, 3].join(":")         == "1:2:3"
+       #     assert [1..3].join(":")            == "1:2:3"
        fun join(sep: String): String
        do
                if is_empty then return ""
@@ -909,6 +949,11 @@ redef class Map[K,V]
        # Concatenate couple of 'key value'.
        # key and value are separated by `couple_sep`.
        # each couple is separated each couple with `sep`.
+       #
+       #     var m = new ArrayMap[Int, String]
+       #     m[1] = "one"
+       #     m[10] = "ten"
+       #     assert m.join("; ", "=") == "1=one; 10=ten"
        fun join(sep: String, couple_sep: String): String
        do
                if is_empty then return ""