Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 129 lines (119 sloc) 4.406 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
;; test_array.nu
;; tests for Nu array extensions.
;;
;; Copyright (c) 2007 Tim Burks, Radtastical Inc.

(class TestArray is NuTestCase
 
 (- testCreate is
    (set a (NSMutableArray arrayWithList:'(1 2)))
    (a << "three")
    (assert_equal 3 (a count))
    (assert_equal 2 (a 1))
    (assert_equal "three" (a 2)))
 
 (- testEach is
    (set i 0)
    (set a (array 0 1 2))
    (a each:
       (do (x)
           (assert_equal i x)
           (set i (+ i 1))))
    ;; iteration with break
    (set a (array 0 1 2 3 4 5 6))
    (set sum 0)
    (a each:
       (do (x)
           (if (eq x 4) (break))
           (set sum (+ sum x))))
    (assert_equal 6 sum)
    ;; iteration with continue
    (set a (array 0 1 2 3 4 5 6))
    (set sum 0)
    (a each:
       (do (x)
           (if (eq x 4) (continue))
           (set sum (+ sum x))))
    (assert_equal 17 sum))
 
 (- testEachInReverse is
    (set i 0)
    (set a (array 2 1 0))
    (a eachInReverse:
       (do (x)
           (assert_equal i x)
           (set i (+ i 1))))
    ;; iteration with break
    (set a (array 6 5 4 3 2 1 0))
    (set sum 0)
    (a eachInReverse:
       (do (x)
           (if (eq x 4) (break))
           (set sum (+ sum x))))
    (assert_equal 6 sum)
    ;; iteration with continue
    (set a (array 6 5 4 3 2 1 0))
    (set sum 0)
    (a eachInReverse:
       (do (x)
           (if (eq x 4) (continue))
           (set sum (+ sum x))))
    (assert_equal 17 sum))
 
 (- testEachWithIndex is
    (set i 0)
    (set a (array "zero" "one" "two"))
    (a eachWithIndex:
       (do (value index)
           (assert_equal i index)
           (set i (+ i 1))))
    ;; iteration with break
    (set a (array "zero" "one" "two" "three" "four" "five" "six"))
    (set sum 0)
    (a eachWithIndex:
       (do (value index)
           (if (eq index 4) (break))
           (set sum (+ sum index))))
    (assert_equal 6 sum)
    ;; iteration with continue
    (set a (array "zero" "one" "two" "three" "four" "five" "six"))
    (set sum 0)
    (a eachWithIndex:
       (do (value index)
           (if (eq index 4) (continue))
           (set sum (+ sum index))))
    (assert_equal 17 sum))
 
 (- testSortedArrayUsingBlock is
    (set array (NSArray arrayWithList:(list 9 42 37 1 17 30 11 28)))
    (set sorted (array sortedArrayUsingBlock:(do (a b) (b compare:a))))
    (assert_equal '(42 37 30 28 17 11 9 1) (sorted list))
    (set array (NSArray arrayWithList:(list "mary" "christopher" "ed" "brian" "tim" "jennifer")))
    (set sorted (array sortedArrayUsingBlock:(do (a b) ((a length) compare:(b length)))))
    (assert_equal '("ed" "tim" "mary" "brian" "jennifer" "christopher") (sorted list)))
 
 (- testSortUsingBlock is
    (set array1 (NSArray arrayWithList:(list 9 42 37 1 17 30 11 28)))
    (array1 sortUsingBlock:(do (a b) (b compare:a)))
    (assert_equal '(42 37 30 28 17 11 9 1) (array1 list))
    (set array2 (NSArray arrayWithList:(list "mary" "christopher" "ed" "brian" "tim" "jennifer")))
    (array2 sortUsingBlock:(do (a b) ((a length) compare:(b length))))
    (assert_equal '("ed" "tim" "mary" "brian" "jennifer" "christopher") (array2 list)))
 
 (- testSortedArrayUsingSelector is
    ;; I don't like this, but want to be sure we bridge the right
    ;; return type for the comparison method. On Snow Leopard (at
    ;; least), NSInteger is either int or long depending on the
    ;; build architecture.
    (case ((NSNumber instanceMethodWithName:"compare:") returnType)
          ("i" (class NSObject (- (int) reverseCompare:(id) other is (* -1 (self compare:other)))))
          ("q" (class NSObject (- (long) reverseCompare:(id) other is (* -1 (self compare:other))))))
    (set a (array 1 9 2 8 3 7 4 6 5))
    (set sorted (a sortedArrayUsingSelector:"reverseCompare:"))
    (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted)
    ;; this is better. We directly support NSComparisonResult in declarations.
    (class NSObject (- (NSComparisonResult) reverseCompare2:(id) other is (* -1 (self compare:other))))
    (set sorted2 (a sortedArrayUsingSelector:"reverseCompare2:"))
    (assert_equal (array 9 8 7 6 5 4 3 2 1) sorted2))
 
 (- testIndexing is
    (set a (array 1 2 3))
    (assert_equal 1 (a 0))
    (assert_equal 2 (a 1))
    (assert_equal 3 (a (+ 1 1)))
    (assert_equal 3 (a -1))
    (assert_equal 2 (a -2))
    (assert_equal 1 (a -3))
    (assert_equal nil (a 3))
    (assert_equal nil (a -4))))
Something went wrong with that request. Please try again.