public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_comparison.nu
100644 60 lines (49 sloc) 2.178 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;; test_comparison.nu
;; tests for Nu comparison operators.
;;
;; Copyright (c) 2008 Tim Burks, Neon Design Technology, Inc.
 
(class TestComparison is NuTestCase
     
     (- testLessThan is
        (assert_equal t (< 1 2 3))
        (assert_equal t (< "a" "b" "c"))
        (assert_equal nil (< 1 2 3 3))
        (assert_equal nil (< "b" "a")))
     
     (- testGreaterThan is
        (assert_equal t (> 3 2 1))
        (assert_equal t (> "c" "b" "a"))
        (assert_equal nil (> 3 2 1 1))
        (assert_equal nil (> "a" "b")))
     
     (- testLessThanOrEqual is
        (assert_equal t (<= 1 2 2 3))
        (assert_equal t (<= "a" "b" "b" "c"))
        (assert_equal nil (<= 1 2 3 2))
        (assert_equal nil (<= "b" "a")))
     
     (- testGreaterThanOrEqual is
        (assert_equal t (>= 3 2 2 1))
        (assert_equal t (>= "c" "b" "b" "a"))
        (assert_equal nil (>= 3 2 1 2))
        (assert_equal nil (>= "a" "b")))
     
     (if (eq (uname) "Darwin") ;; I think the problem here is with method declaration
         (- testCustomComparison is
            
            (class NumericString is NSObject
                 (ivar (id) string)
                 (+ stringWithString:s is ((self alloc) initWithString:s))
                 (- initWithString:s is
                    (self init)
                    (set @string s)
                    self)
                 (- stringValue is @string)
                 (- description is @string)
                 (- (int) compare:(id) other is
                    ((@string intValue) compare:((other stringValue) intValue))))
            
            (set x (NumericString stringWithString:"123"))
            (set y (NumericString stringWithString:"45"))
            (set z (NumericString stringWithString:"12"))
            
            (set a ((array x y z) sort))
            
            (assert_equal "12" ((a 0) stringValue))
            (assert_equal "45" ((a 1) stringValue))
            (assert_equal "123" ((a 2) stringValue))
            
            (assert_equal nil (<= x y))
            (assert_equal nil (< x y))
            (assert_equal t (>= x y))
            (assert_equal t (> x y)))))