[前][次][番号順一覧][スレッド一覧]

ruby-changes:4523

From: ko1@a...
Date: Mon, 14 Apr 2008 20:04:14 +0900 (JST)
Subject: [ruby-changes:4523] knu - Ruby:r16016 (ruby_1_8): * array.c (rb_ary_flatten, rb_ary_flatten_bang): Take an optional

knu	2008-04-14 20:03:42 +0900 (Mon, 14 Apr 2008)

  New Revision: 16016

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/array.c
    branches/ruby_1_8/test/ruby/test_array.rb

  Log:
    * array.c (rb_ary_flatten, rb_ary_flatten_bang): Take an optional
      argument that determines the level of recursion to flatten;
      backported from 1.9.
    
    * array.c (rb_ary_shuffle_bang, rb_ary_shuffle, rb_ary_choice,
      rb_ary_cycle, rb_ary_permutation, rb_ary_combination,
      rb_ary_product, rb_ary_take, rb_ary_take_while, rb_ary_drop,
      rb_ary_drop_while): New methods: Array#shuffle, #shuffle!,
      #choice, #cycle, #permutation, #combination, #product, #take,
      #take_while, #drop, #drop_while; backported from 1.9.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16016&r2=16015&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/array.c?r1=16016&r2=16015&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/test/ruby/test_array.rb?r1=16016&r2=16015&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/NEWS?r1=16016&r2=16015&diff_format=u

Index: ruby_1_8/array.c
===================================================================
--- ruby_1_8/array.c	(revision 16015)
+++ ruby_1_8/array.c	(revision 16016)
@@ -960,16 +960,16 @@
 
     if (argc  == 0) {
 	RETURN_ENUMERATOR(ary, 0, 0);
-	for (i=0; i<RARRAY_LEN(ary); i++) {
-	    if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) {
+	for (i=0; i<RARRAY(ary)->len; i++) {
+	    if (RTEST(rb_yield(RARRAY(ary)->ptr[i]))) {
 		return LONG2NUM(i);
 	    }
 	}
 	return Qnil;
     }
     rb_scan_args(argc, argv, "01", &val);
-    for (i=0; i<RARRAY_LEN(ary); i++) {
-	if (rb_equal(RARRAY_PTR(ary)[i], val))
+    for (i=0; i<RARRAY(ary)->len; i++) {
+	if (rb_equal(RARRAY(ary)->ptr[i], val))
 	    return LONG2NUM(i);
     }
     return Qnil;
@@ -997,25 +997,25 @@
     VALUE ary;
 {
     VALUE val;
-    long i = RARRAY_LEN(ary);
+    long i = RARRAY(ary)->len;
 
     if (argc == 0) {
 	RETURN_ENUMERATOR(ary, 0, 0);
 	while (i--) {
-	    if (RTEST(rb_yield(RARRAY_PTR(ary)[i])))
+	    if (RTEST(rb_yield(RARRAY(ary)->ptr[i])))
 		return LONG2NUM(i);
-	    if (i > RARRAY_LEN(ary)) {
-		i = RARRAY_LEN(ary);
+	    if (i > RARRAY(ary)->len) {
+		i = RARRAY(ary)->len;
 	    }
 	}
 	return Qnil;
     }
     rb_scan_args(argc, argv, "01", &val);
     while (i--) {
-	if (rb_equal(RARRAY_PTR(ary)[i], val))
+	if (rb_equal(RARRAY(ary)->ptr[i], val))
 	    return LONG2NUM(i);
-	if (i > RARRAY_LEN(ary)) {
-	    i = RARRAY_LEN(ary);
+	if (i > RARRAY(ary)->len) {
+	    i = RARRAY(ary)->len;
 	}
     }
     return Qnil;
@@ -2094,7 +2094,7 @@
     }
 
     if (!FIXNUM_P(arg1)) {
-	switch (rb_range_beg_len(arg1, &pos, &len, RARRAY_LEN(ary), 0)) {
+	switch (rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 0)) {
 	  case Qtrue:
 	    /* valid range */
 	    goto delete_pos_len;
@@ -2556,9 +2556,9 @@
     VALUE v;
 
     for (i = 0; i < RARRAY(ary)->len; ++i) {
-	v = rb_check_array_type(RARRAY_PTR(ary)[i]);
+	v = rb_check_array_type(RARRAY(ary)->ptr[i]);
 	if (!NIL_P(v) && RARRAY(v)->len > 0 &&
-	    rb_equal(RARRAY_PTR(v)[0], key))
+	    rb_equal(RARRAY(v)->ptr[0], key))
 	    return v;
     }
     return Qnil;
@@ -3002,14 +3002,14 @@
     if (rb_block_given_p()) {
 	long i;
 
-	for (i=0; i<RARRAY_LEN(ary); i++) {
-	    VALUE v = RARRAY_PTR(ary)[i];
+	for (i=0; i<RARRAY(ary)->len; i++) {
+	    VALUE v = RARRAY(ary)->ptr[i];
  	    if (RTEST(rb_yield(v))) n++;
  	}
     }
     else {
-	VALUE *p = RARRAY_PTR(ary);
-	VALUE *pend = p + RARRAY_LEN(ary);
+	VALUE *p = RARRAY(ary)->ptr;
+	VALUE *pend = p + RARRAY(ary)->len;
 
  	while (p < pend) {
  	    if (!NIL_P(*p)) n++;
@@ -3019,101 +3019,631 @@
     return LONG2NUM(n);
 }
 
-static long
-flatten(ary, idx, ary2, memo)
+static VALUE
+flatten(ary, level, modified)
     VALUE ary;
-    long idx;
-    VALUE ary2, memo;
+    int level;
+    int *modified;
 {
-    VALUE id;
-    long i = idx;
-    long n, lim = idx + RARRAY(ary2)->len;
+    long i = 0;
+    VALUE stack, result, tmp, elt;
+    st_table *memo;
+    st_data_t id;
 
-    id = rb_obj_id(ary2);
-    if (rb_ary_includes(memo, id)) {
-	rb_raise(rb_eArgError, "tried to flatten recursive array");
-    }
-    rb_ary_push(memo, id);
-    rb_ary_splice(ary, idx, 1, ary2);
-    while (i < lim) {
-	VALUE tmp;
+    stack = rb_ary_new();
+    result = rb_ary_new();
+    memo = st_init_numtable();
+    st_insert(memo, (st_data_t)ary, (st_data_t)Qtrue);
+    *modified = 0;
 
-	tmp = rb_check_array_type(rb_ary_elt(ary, i));
-	if (!NIL_P(tmp)) {
-	    n = flatten(ary, i, tmp, memo);
-	    i += n; lim += n;
+    while (1) {
+	while (i < RARRAY(ary)->len) {
+	    elt = RARRAY(ary)->ptr[i++];
+	    tmp = rb_check_array_type(elt);
+	    if (NIL_P(tmp) || (level >= 0 && RARRAY(stack)->len / 2 >= level)) {
+		rb_ary_push(result, elt);
+	    }
+	    else {
+		*modified = 1;
+		id = (st_data_t)tmp;
+		if (st_lookup(memo, id, 0)) {
+		    rb_raise(rb_eArgError, "tried to flatten recursive array");
+		}
+		st_insert(memo, id, (st_data_t)Qtrue);
+		rb_ary_push(stack, ary);
+		rb_ary_push(stack, LONG2NUM(i));
+		ary = tmp;
+		i = 0;
+	    }
 	}
-	i++;
+	if (RARRAY(stack)->len == 0) {
+	    break;
+	}
+	id = (st_data_t)ary;
+	st_delete(memo, &id, 0);
+	tmp = rb_ary_pop(stack);
+	i = NUM2LONG(tmp);
+	ary = rb_ary_pop(stack);
     }
-    rb_ary_pop(memo);
 
-    return lim - idx - 1;	/* returns number of increased items */
+    return result;
 }
 
 /*
  *  call-seq:
  *     array.flatten! -> array or nil
+ *     array.flatten!(level) -> array or nil
  *  
  *  Flattens _self_ in place.
  *  Returns <code>nil</code> if no modifications were made (i.e.,
- *  <i>array</i> contains no subarrays.)
+ *  <i>array</i> contains no subarrays.)  If the optional <i>level</i>
+ *  argument determines the level of recursion to flatten.
  *     
  *     a = [ 1, 2, [3, [4, 5] ] ]
  *     a.flatten!   #=> [1, 2, 3, 4, 5]
  *     a.flatten!   #=> nil
  *     a            #=> [1, 2, 3, 4, 5]
+ *     a = [ 1, 2, [3, [4, 5] ] ]
+ *     a.flatten!(1) #=> [1, 2, 3, [4, 5]]
  */
 
 static VALUE
-rb_ary_flatten_bang(ary)
+rb_ary_flatten_bang(argc, argv, ary)
+    int argc;
+    VALUE *argv;
     VALUE ary;
 {
-    long i = 0;
-    int mod = 0;
-    VALUE memo = Qnil;
+    int mod = 0, level = -1;
+    VALUE result, lv;
 
-    while (i<RARRAY(ary)->len) {
-	VALUE ary2 = RARRAY(ary)->ptr[i];
-	VALUE tmp;
+    rb_scan_args(argc, argv, "01", &lv);
+    if (!NIL_P(lv)) level = NUM2INT(lv);
+    if (level == 0) return ary;
 
-	tmp = rb_check_array_type(ary2);
-	if (!NIL_P(tmp)) {
-	    if (NIL_P(memo)) {
-		memo = rb_ary_new();
-	    }
-	    i += flatten(ary, i, tmp, memo);
-	    mod = 1;
-	}
-	i++;
-    }
+    result = flatten(ary, level, &mod);
     if (mod == 0) return Qnil;
+    rb_ary_replace(ary, result);
+
     return ary;
 }
 
 /*
  *  call-seq:
  *     array.flatten -> an_array
+ *     array.flatten(level) -> an_array
  *  
  *  Returns a new array that is a one-dimensional flattening of this
  *  array (recursively). That is, for every element that is an array,
- *  extract its elements into the new array.
+ *  extract its elements into the new array.  If the optional
+ *  <i>level</i> argument determines the level of recursion to flatten.
  *     
  *     s = [ 1, 2, 3 ]           #=> [1, 2, 3]
  *     t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
  *     a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
- *     a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
+ *     a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ *     a = [ 1, 2, [3, [4, 5] ] ]
+ *     a.flatten(1)              #=> [1, 2, 3, [4, 5]]
  */
 
 static VALUE
-rb_ary_flatten(ary)
+rb_ary_flatten(argc, argv, ary)
+    int argc;
+    VALUE *argv;
     VALUE ary;
 {
+    int mod = 0, level = -1;
+    VALUE result, lv;
+
+    rb_scan_args(argc, argv, "01", &lv);
+    if (!NIL_P(lv)) level = NUM2INT(lv);
+    if (level == 0) return ary;
+
+    result = flatten(ary, level, &mod);
+    if (OBJ_TAINTED(ary)) OBJ_TAINT(result);
+
+    return result;
+}
+
+/*
+ *  call-seq:
+ *     array.shuffle!        -> array or nil
+ *  
+ *  Shuffles elements in _self_ in place.
+ */
+
+
+static VALUE
+rb_ary_shuffle_bang(ary)
+    VALUE ary;
+{
+    long i = RARRAY(ary)->len;
+
+    rb_ary_modify(ary);
+    while (i) {
+	long j = rb_genrand_real()*i;
+	VALUE tmp = RARRAY(ary)->ptr[--i];
+	RARRAY(ary)->ptr[i] = RARRAY(ary)->ptr[j];
+	RARRAY(ary)->ptr[j] = tmp;
+    }
+    return ary;
+}
+
+
+/*
+ *  call-seq:
+ *     array.shuffle -> an_array
+ *  
+ *  Returns a new array with elements of this array shuffled.
+ *     
+ *     a = [ 1, 2, 3 ]           #=> [1, 2, 3]
+ *     a.shuffle                 #=> [2, 3, 1]
+ */
+
+static VALUE
+rb_ary_shuffle(VALUE ary)
+{
     ary = rb_ary_dup(ary);
-    rb_ary_flatten_bang(ary);
+    rb_ary_shuffle_bang(ary);
     return ary;
 }
 
 
+/*
+ *  call-seq:
+ *     array.choice        -> obj
+ *  
+ *  Choose a random element from an array.
+ */
+
+
+static VALUE
+rb_ary_choice(ary)
+    VALUE ary;
+{
+    long i, j;
+
+    i = RARRAY(ary)->len;
+    if (i == 0) return Qnil;
+    j = rb_genrand_real()*i;
+    return RARRAY(ary)->ptr[j];
+}
+
+
+/*
+ *  call-seq:
+ *     ary.cycle {|obj| block }
+ *     ary.cycle(n) {|obj| block }
+ *  
+ *  Calls <i>block</i> for each element repeatedly _n_ times or
+ *  forever if none or nil is given.  If a non-positive number is
+ *  given or the array is empty, does nothing.  Returns nil if the
+ *  loop has finished without getting interrupted.
+ *     
+ *     a = ["a", "b", "c"]
+ *     a.cycle {|x| puts x }  # print, a, b, c, a, b, c,.. forever.
+ *     a.cycle(2) {|x| puts x }  # print, a, b, c, a, b, c.
+ *     
+ */
+
+static VALUE
+rb_ary_cycle(argc, argv, ary)
+    int argc;
+    VALUE *argv;
+    VALUE ary;
+{
+    long n, i;
+    VALUE nv = Qnil;
+
+    rb_scan_args(argc, argv, "01", &nv);
+
+    RETURN_ENUMERATOR(ary, argc, argv);
+    if (NIL_P(nv)) {
+        n = -1;
+    }
+    else {
+        n = NUM2LONG(nv);
+        if (n <= 0) return Qnil;
+    }
+
+    while (RARRAY(ary)->len > 0 && (n < 0 || 0 < n--)) {
+        for (i=0; i<RARRAY(ary)->len; i++) {
+            rb_yield(RARRAY(ary)->ptr[i]);
+        }
+    }
+    return Qnil;
+}
+
+#define tmpbuf(n, size) rb_str_tmp_new((n)*(size))
+
+/*
+ * Recursively compute permutations of r elements of the set [0..n-1].
+ * When we have a complete permutation of array indexes, copy the values
+ * at those indexes into a new array and yield that array. 
+ *
+ * n: the size of the set 
+ * r: the number of elements in each permutation
+ * p: the array (of size r) that we're filling in
+ * index: what index we're filling in now
+ * used: an array of booleans: whether a given index is already used
+ * values: the Ruby array that holds the actual values to permute
+ */
+static void
+permute0(n, r, p, index, used, values)
+    long n, r, *p, index;
+    int *used;
+    VALUE values;
+{
+    long i,j;
+    for (i = 0; i < n; i++) {
+	if (used[i] == 0) {
+	    p[index] = i;
+	    if (index < r-1) {             /* if not done yet */
+		used[i] = 1;               /* mark index used */
+		permute0(n, r, p, index+1, /* recurse */
+			 used, values);  
+		used[i] = 0;               /* index unused */
+	    }
+	    else {
+		/* We have a complete permutation of array indexes */
+		/* Build a ruby array of the corresponding values */
+		/* And yield it to the associated block */
+		VALUE result = rb_ary_new2(r);
+		VALUE *result_array = RARRAY(result)->ptr;
+		const VALUE *values_array = RARRAY(values)->ptr;
+
+		for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
+		RARRAY(result)->len = r;
+		rb_yield(result);
+	    }
+	}
+    }
+}
+
+/*
+ *  call-seq:
+ *     ary.permutation { |p| block }          -> array
+ *     ary.permutation                        -> enumerator
+ *     ary.permutation(n) { |p| block }       -> array
+ *     ary.permutation(n)                     -> enumerator
+ *  
+ * When invoked with a block, yield all permutations of length <i>n</i>
+ * of the elements of <i>ary</i>, then return the array itself.
+ * If <i>n</i> is not specified, yield all permutations of all elements.
+ * The implementation makes no guarantees about the order in which 
+ * the permutations are yielded.
+ *
+ * When invoked without a block, return an enumerator object instead.
+ * 
+ * Examples:
+ *
+ *     a = [1, 2, 3]
+ *     a.permutation.to_a     #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
+ *     a.permutation(1).to_a  #=> [[1],[2],[3]]
+ *     a.permutation(2).to_a  #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
+ *     a.permutation(3).to_a  #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
+ *     a.permutation(0).to_a  #=> [[]] # one permutation of length 0
+ *     a.permutation(4).to_a  #=> []   # no permutations of length 4
+ */
+
+static VALUE
+rb_ary_permutation(argc, argv, ary)
+    int argc;
+    VALUE *argv;
+    VALUE ary;
+{
+    VALUE num;
+    long r, n, i;
+
+    n = RARRAY(ary)->len;                  /* Array length */
+    RETURN_ENUMERATOR(ary, argc, argv);   /* Return enumerator if no block */
+    rb_scan_args(argc, argv, "01", &num);
+    r = NIL_P(num) ? n : NUM2LONG(num);   /* Permutation size from argument */
+
+    if (r < 0 || n < r) { 
+	/* no permutations: yield nothing */
+    }
+    else if (r == 0) { /* exactly one permutation: the zero-length array */
+	rb_yield(rb_ary_new2(0));
+    }
+    else if (r == 1) { /* this is a special, easy case */
+	for (i = 0; i < RARRAY(ary)->len; i++) {
+	    rb_yield(rb_ary_new3(1, RARRAY(ary)->ptr[i]));
+	}
+    }
+    else {             /* this is the general case */
+	volatile VALUE t0 = tmpbuf(n,sizeof(long));
+	long *p = (long*)RSTRING(t0)->ptr;
+	volatile VALUE t1 = tmpbuf(n,sizeof(int));
+	int *used = (int*)RSTRING(t1)->ptr;
+	VALUE ary0 = ary_make_shared(ary); /* private defensive copy of ary */
+
+	for (i = 0; i < n; i++) used[i] = 0; /* initialize array */
+
+	permute0(n, r, p, 0, used, ary0); /* compute and yield permutations */
+	RB_GC_GUARD(t0);
+	RB_GC_GUARD(t1);
+    }
+    return ary;
+}
+
+static long
+combi_len(n, k)
+    long n, k;
+{
+    long i, val = 1;
+
+    if (k*2 > n) k = n-k;
+    if (k == 0) return 1;
+    if (k < 0) return 0;
+    val = 1;
+    for (i=1; i <= k; i++,n--) {
+	long m = val;
+	val *= n;
+	if (val < m) {
+	    rb_raise(rb_eRangeError, "too big for combination");
+	}
+	val /= i;
+    }
+    return val;
+}
+
+/*
+ *  call-seq:
+ *     ary.combination(n) { |c| block }    -> ary
+ *     ary.combination(n)                  -> enumerator
+ *  
+ * When invoked with a block, yields all combinations of length <i>n</i> 
+ * of elements from <i>ary</i> and then returns <i>ary</i> itself.
+ * The implementation makes no guarantees about the order in which 
+ * the combinations are yielded.
+ *
+ * When invoked without a block, returns an enumerator object instead.
+ *     
+ * Examples:
+ *
+ *     a = [1, 2, 3, 4]
+ *     a.combination(1).to_a  #=> [[1],[2],[3],[4]]
+ *     a.combination(2).to_a  #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
+ *     a.combination(3).to_a  #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
+ *     a.combination(4).to_a  #=> [[1,2,3,4]]
+ *     a.combination(0).to_a  #=> [[]] # one combination of length 0
+ *     a.combination(5).to_a  #=> []   # no combinations of length 5
+ *     
+ */
+
+static VALUE
+rb_ary_combination(ary, num)
+    VALUE ary;
+    VALUE num;
+{
+    long n, i, len;
+
+    n = NUM2LONG(num);
+    RETURN_ENUMERATOR(ary, 1, &num);
+    len = RARRAY(ary)->len;
+    if (n < 0 || len < n) {
+	/* yield nothing */
+    }
+    else if (n == 0) {
+	rb_yield(rb_ary_new2(0));
+    }
+    else if (n == 1) {
+	for (i = 0; i < len; i++) {
+	    rb_yield(rb_ary_new3(1, RARRAY(ary)->ptr[i]));
+	}
+    }
+    else {
+	volatile VALUE t0 = tmpbuf(n+1, sizeof(long));
+	long *stack = (long*)RSTRING(t0)->ptr;
+	long nlen = combi_len(len, n);
+	volatile VALUE cc = rb_ary_new2(n);
+	VALUE *chosen = RARRAY(cc)->ptr;
+	long lev = 0;
+
+	RBASIC(cc)->klass = 0;
+	MEMZERO(stack, long, n);
+	stack[0] = -1;
+	for (i = 0; i < nlen; i++) {
+	    chosen[lev] = RARRAY(ary)->ptr[stack[lev+1]];
+	    for (lev++; lev < n; lev++) {
+		chosen[lev] = RARRAY(ary)->ptr[stack[lev+1] = stack[lev]+1];
+	    }
+	    rb_yield(rb_ary_new4(n, chosen));
+	    do {
+		stack[lev--]++;
+	    } while (lev && (stack[lev+1]+n == len+lev+1));
+	}
+    }
+    return ary;
+}
+
+/*
+ *  call-seq:
+ *     ary.product(other_ary, ...)
+ *  
+ *  Returns an array of all combinations of elements from all arrays.
+ *  The length of the returned array is the product of the length
+ *  of ary and the argument arrays
+ *     
+ *     [1,2,3].product([4,5])     # => [[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]]
+ *     [1,2].product([1,2])       # => [[1,1],[1,2],[2,1],[2,2]]
+ *     [1,2].product([3,4],[5,6]) # => [[1,3,5],[1,3,6],[1,4,5],[1,4,6],
+ *                                #     [2,3,5],[2,3,6],[2,4,5],[2,4,6]]
+ *     [1,2].product()            # => [[1],[2]]
+ *     [1,2].product([])          # => []
+ */
+
+static VALUE
+rb_ary_product(argc, argv, ary)
+    int argc;
+    VALUE *argv;
+    VALUE ary;
+{
+    int n = argc+1;    /* How many arrays we're operating on */
+    volatile VALUE t0 = tmpbuf(n, sizeof(VALUE));
+    volatile VALUE t1 = tmpbuf(n, sizeof(int));
+    VALUE *arrays = (VALUE*)RSTRING(t0)->ptr; /* The arrays we're computing the product of */
+    int *counters = (int*)RSTRING(t1)->ptr; /* The current position in each one */
+    VALUE result;      /* The array we'll be returning */
+    long i,j;
+    long resultlen = 1;
+
+    RBASIC(t0)->klass = 0;
+    RBASIC(t1)->klass = 0;
+
+    /* initialize the arrays of arrays */
+    arrays[0] = ary;
+    for (i = 1; i < n; i++) arrays[i] = to_ary(argv[i-1]);
+    
+    /* initialize the counters for the arrays */
+    for (i = 0; i < n; i++) counters[i] = 0;
+
+    /* Compute the length of the result array; return [] if any is empty */
+    for (i = 0; i < n; i++) {
+	long k = RARRAY(arrays[i])->len, l = resultlen;
+	if (k == 0) return rb_ary_new2(0);
+	resultlen *= k;
+	if (resultlen < k || resultlen < l || resultlen / k != l) {
+	    rb_raise(rb_eRangeError, "too big to product");
+	}
+    }
+
+    /* Otherwise, allocate and fill in an array of results */
+    result = rb_ary_new2(resultlen);
+    for (i = 0; i < resultlen; i++) {
+	int m;
+	/* fill in one subarray */
+	VALUE subarray = rb_ary_new2(n);
+	for (j = 0; j < n; j++) {
+	    rb_ary_push(subarray, rb_ary_entry(arrays[j], counters[j]));
+	}
+
+	/* put it on the result array */
+	rb_ary_push(result, subarray);
+
+	/*
+	 * Increment the last counter.  If it overflows, reset to 0
+	 * and increment the one before it.
+	 */
+	m = n-1;
+	counters[m]++;
+	while (m > 0 && counters[m] == RARRAY(arrays[m])->len) {
+	    counters[m] = 0;
+	    m--;
+	    counters[m]++;
+	}
+    }
+
+    return result;
+}
+
+/*
+ *  call-seq:
+ *     ary.take(n)               => array
+ *  
+ *  Returns first n elements from <i>ary</i>.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.take(3)             # => [1, 2, 3]
+ *     
+ */
+
+static VALUE
+rb_ary_take(obj, n)
+    VALUE obj;
+    VALUE n;
+{
+    long len = NUM2LONG(n);
+    if (len < 0) {
+	rb_raise(rb_eArgError, "attempt to take negative size");
+    }
+
+    return rb_ary_subseq(obj, 0, len);
+}
+
+/*
+ *  call-seq:
+ *     ary.take_while {|arr| block }   => array
+ *  
+ *  Passes elements to the block until the block returns nil or false,
+ *  then stops iterating and returns an array of all prior elements.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.take_while {|i| i < 3 }   # => [1, 2]
+ *     
+ */
+
+static VALUE
+rb_ary_take_while(ary)
+    VALUE ary;
+{
+    VALUE result;
+    long i;
+
+    RETURN_ENUMERATOR(ary, 0, 0);
+    for (i = 0; i < RARRAY(ary)->len; i++) {
+	if (!RTEST(rb_yield(RARRAY(ary)->ptr[i]))) break;
+    }
+    return rb_ary_take(ary, LONG2FIX(i));
+}
+
+/*
+ *  call-seq:
+ *     ary.drop(n)               => array
+ *  
+ *  Drops first n elements from <i>ary</i>, and returns rest elements
+ *  in an array.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.drop(3)             # => [4, 5, 0]
+ *     
+ */
+
+static VALUE
+rb_ary_drop(ary, n)
+    VALUE ary;
+    VALUE n;
+{
+    VALUE result;
+    long pos = NUM2LONG(n);
+    if (pos < 0) {
+	rb_raise(rb_eArgError, "attempt to drop negative size");
+    }
+
+    result = rb_ary_subseq(ary, pos, RARRAY(ary)->len);
+    if (result == Qnil) result = rb_ary_new();
+    return result;
+}
+
+/*
+ *  call-seq:
+ *     ary.drop_while {|arr| block }   => array
+ *  
+ *  Drops elements up to, but not including, the first element for
+ *  which the block returns nil or false and returns an array
+ *  containing the remaining elements.
+ *     
+ *     a = [1, 2, 3, 4, 5, 0]
+ *     a.drop_while {|i| i < 3 }   # => [3, 4, 5, 0]
+ *     
+ */
+
+static VALUE
+rb_ary_drop_while(ary)
+    VALUE ary;
+{
+    VALUE result;
+    long i;
+
+    RETURN_ENUMERATOR(ary, 0, 0);
+    for (i = 0; i < RARRAY(ary)->len; i++) {
+	if (!RTEST(rb_yield(RARRAY(ary)->ptr[i]))) break;
+    }
+    return rb_ary_drop(ary, LONG2FIX(i));
+}
+
+
+
 /* Arrays are ordered, integer-indexed collections of any object. 
  * Array indexing starts at 0, as in C or Java.  A negative index is 
  * assumed to be relative to the end of the array---that is, an index of -1 
@@ -3207,10 +3737,22 @@
     rb_define_method(rb_cArray, "uniq!", rb_ary_uniq_bang, 0);
     rb_define_method(rb_cArray, "compact", rb_ary_compact, 0);
     rb_define_method(rb_cArray, "compact!", rb_ary_compact_bang, 0);
-    rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);
-    rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, 0);
+    rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
+    rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
     rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
+    rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
+    rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);
+    rb_define_method(rb_cArray, "choice", rb_ary_choice, 0);
+    rb_define_method(rb_cArray, "cycle", rb_ary_cycle, -1);
+    rb_define_method(rb_cArray, "permutation", rb_ary_permutation, -1);
+    rb_define_method(rb_cArray, "combination", rb_ary_combination, 1);
+    rb_define_method(rb_cArray, "product", rb_ary_product, -1);
 
+    rb_define_method(rb_cArray, "take", rb_ary_take, 1);
+    rb_define_method(rb_cArray, "take_while", rb_ary_take_while, 0);
+    rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
+    rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
+
     id_cmp = rb_intern("<=>");
     inspect_key = rb_intern("__inspect_key__");
 }
Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 16015)
+++ ruby_1_8/NEWS	(revision 16016)
@@ -21,6 +21,12 @@
     determine if each element should be counted instead of checking if
     the element is non-nil.
 
+  * Array#flatten
+  * Array#flatten!
+
+    Takes an optional argument that determines the level of recursion
+    to flatten.
+
   * Array#index
   * Array#rindex
 
@@ -43,6 +49,20 @@
     Take an optional argument specifying the number of elements to
     remove.
 
+  * Array#choice
+  * Array#combination
+  * Array#cycle
+  * Array#drop
+  * Array#drop_while
+  * Array#permutation
+  * Array#product
+  * Array#shuffle
+  * Array#shuffle!
+  * Array#take,
+  * Array#take_while
+
+    New methods.
+
   * Dir#each
   * Dir#foreach
 
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 16015)
+++ ruby_1_8/ChangeLog	(revision 16016)
@@ -1,3 +1,16 @@
+Mon Apr 14 19:54:21 2008  Akinori MUSHA  <knu@i...>
+
+	* array.c (rb_ary_flatten, rb_ary_flatten_bang): Take an optional
+	  argument that determines the level of recursion to flatten;
+	  backported from 1.9.
+
+	* array.c (rb_ary_shuffle_bang, rb_ary_shuffle, rb_ary_choice,
+	  rb_ary_cycle, rb_ary_permutation, rb_ary_combination,
+	  rb_ary_product, rb_ary_take, rb_ary_take_while, rb_ary_drop,
+	  rb_ary_drop_while): New methods: Array#shuffle, #shuffle!,
+	  #choice, #cycle, #permutation, #combination, #product, #take,
+	  #take_while, #drop, #drop_while; backported from 1.9.
+
 Mon Apr 14 19:52:35 2008  Akinori MUSHA  <knu@i...>
 
 	* ruby.h: New macro: RB_GC_GUARD().
Index: ruby_1_8/test/ruby/test_array.rb
===================================================================
--- ruby_1_8/test/ruby/test_array.rb	(revision 16015)
+++ ruby_1_8/test/ruby/test_array.rb	(revision 16016)
@@ -1,7 +1,7 @@
 require 'test/unit'
 
 class TestArray < Test::Unit::TestCase
-  def test_array
+  def test_0_literal
     assert_equal([1, 2, 3, 4], [1, 2] + [3, 4])
     assert_equal([1, 2, 1, 2], [1, 2] * 2)
     assert_equal("1:2", [1, 2] * ":")
@@ -27,29 +27,32 @@
     assert(x[-1] == 20 && x.pop == 20)
   end
 
-  def test_array_andor
+  def test_array_andor_0
     assert_equal([2], ([1,2,3]&[2,4,6]))
     assert_equal([1,2,3,4,6], ([1,2,3]|[2,4,6]))
   end
 
-  def test_compact
-    x = [nil, 1, nil, nil, 5, nil, nil]
-    x.compact!
-    assert_equal([1, 5], x)
+  def test_compact_0
+    a = [nil, 1, nil, nil, 5, nil, nil]
+    assert_equal [1, 5], a.compact
+    assert_equal [nil, 1, nil, nil, 5, nil, nil], a
+    a.compact!
+    assert_equal [1, 5], a
   end
 
-  def test_uniq
+  def test_uniq_0
     x = [1, 1, 4, 2, 5, 4, 5, 1, 2]
     x.uniq!
     assert_equal([1, 4, 2, 5], x)
+  end
 
-    # empty?
-    assert(!x.empty?)
-    x = []
-    assert(x.empty?)
+  def test_empty_0
+    assert_equal true, [].empty?
+    assert_equal false, [1].empty?
+    assert_equal false, [1, 1, 4, 2, 5, 4, 5, 1, 2].empty?
   end
 
-  def test_sort
+  def test_sort_0
     x = ["it", "came", "to", "pass", "that", "..."]
     x = x.sort.join(" ")
     assert_equal("... came it pass that to", x)
@@ -60,7 +63,7 @@
     assert_equal([7,5,3,2,1], x)
   end
 
-  def test_split
+  def test_split_0
     x = "The Boassert of Mormon"
     assert_equal(x.reverse, x.split(//).reverse!.join)
     assert_equal(x.reverse, x.reverse!)
@@ -70,7 +73,7 @@
     assert_equal(['a', 'b', 'c', 'd'], x.split(' '))
   end
 
-  def test_misc
+  def test_misc_0
     assert(defined? "a".chomp)
     assert_equal(["a", "b", "c"], "abc".scan(/./))
     assert_equal([["1a"], ["2b"], ["3c"]], "1a2b3c".scan(/(\d.)/))
@@ -110,7 +113,35 @@
     assert_equal([1,2,3,5], y)
   end
 
-  def test_find_all
+  def test_beg_end_0
+    x = [1, 2, 3, 4, 5]
+    
+    assert_equal(1, x.first)
+    assert_equal([1], x.first(1))
+    assert_equal([1, 2, 3], x.first(3))
+    
+    assert_equal(5, x.last)
+    assert_equal([5], x.last(1))
+    assert_equal([3, 4, 5], x.last(3))
+    
+    assert_equal(1, x.shift)
+    assert_equal([2, 3, 4], x.shift(3))
+    assert_equal([5], x)
+    
+    assert_equal([2, 3, 4, 5], x.unshift(2, 3, 4))
+    assert_equal([1, 2, 3, 4, 5], x.unshift(1))
+    assert_equal([1, 2, 3, 4, 5], x)
+    
+    assert_equal(5, x.pop)
+    assert_equal([3, 4], x.pop(2))
+    assert_equal([1, 2], x)
+    
+    assert_equal([1, 2, 3, 4], x.push(3, 4))
+    assert_equal([1, 2, 3, 4, 5], x.push(5))
+    assert_equal([1, 2, 3, 4, 5], x)
+  end
+
+  def test_find_all_0
     assert_respond_to([], :find_all)
     assert_respond_to([], :select)       # Alias
     assert_equal([], [].find_all{ |obj| obj == "foo"})
@@ -120,7 +151,7 @@
     assert_equal([3,3], x.find_all{ |obj| obj == 3 })
   end
 
-  def test_fill
+  def test_fill_0
     assert_equal([-1, -1, -1, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1))
     assert_equal([0, 1, 2, -1, -1, -1], [0, 1, 2, 3, 4, 5].fill(-1, 3))
     assert_equal([0, 1, 2, -1, -1, 5], [0, 1, 2, 3, 4, 5].fill(-1, 3, 2))
@@ -149,6 +180,833 @@
     @cls = Array
   end
 
+  def test_00_new
+    a = @cls.new()
+    assert_instance_of(@cls, a)
+    assert_equal(0, a.length)
+    assert_nil(a[0])
+  end
+
+  def test_01_square_brackets
+    a = @cls[ 5, 4, 3, 2, 1 ]
+    assert_instance_of(@cls, a)
+    assert_equal(5, a.length)
+    5.times { |i| assert_equal(5-i, a[i]) }
+    assert_nil(a[6])
+  end
+
+  def test_AND # '&'
+    assert_equal(@cls[1, 3], @cls[ 1, 1, 3, 5 ] & @cls[ 1, 2, 3 ])
+    assert_equal(@cls[],     @cls[ 1, 1, 3, 5 ] & @cls[ ])
+    assert_equal(@cls[],     @cls[  ]           & @cls[ 1, 2, 3 ])
+    assert_equal(@cls[],     @cls[ 1, 2, 3 ]    & @cls[ 4, 5, 6 ])
+  end
+
+  def test_MUL # '*'
+    assert_equal(@cls[], @cls[]*3)
+    assert_equal(@cls[1, 1, 1], @cls[1]*3)
+    assert_equal(@cls[1, 2, 1, 2, 1, 2], @cls[1, 2]*3)
+    assert_equal(@cls[], @cls[1, 2, 3] * 0)
+    assert_raise(ArgumentError) { @cls[1, 2]*(-3) }
+
+    assert_equal('1-2-3-4-5', @cls[1, 2, 3, 4, 5] * '-')
+    assert_equal('12345',     @cls[1, 2, 3, 4, 5] * '')
+
+  end
+
+  def test_PLUS # '+'
+    assert_equal(@cls[],     @cls[]  + @cls[])
+    assert_equal(@cls[1],    @cls[1] + @cls[])
+    assert_equal(@cls[1],    @cls[]  + @cls[1])
+    assert_equal(@cls[1, 1], @cls[1] + @cls[1])
+    assert_equal(@cls['cat', 'dog', 1, 2, 3], %w(cat dog) + (1..3).to_a)
+  end
+
+  def test_MINUS # '-'
+    assert_equal(@cls[],  @cls[1] - @cls[1])
+    assert_equal(@cls[1], @cls[1, 2, 3, 4, 5] - @cls[2, 3, 4, 5])
+    # Ruby 1.8 feature change
+    #assert_equal(@cls[1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
+    assert_equal(@cls[1, 1, 1, 1], @cls[1, 2, 1, 3, 1, 4, 1, 5] - @cls[2, 3, 4, 5])
+    a = @cls[]
+    1000.times { a << 1 }
+    assert_equal(1000, a.length)
+    #assert_equal(@cls[1], a - @cls[2])
+    assert_equal(@cls[1] * 1000, a - @cls[2])
+    #assert_equal(@cls[1],  @cls[1, 2, 1] - @cls[2])
+    assert_equal(@cls[1, 1],  @cls[1, 2, 1] - @cls[2])
+    assert_equal(@cls[1, 2, 3], @cls[1, 2, 3] - @cls[4, 5, 6])
+  end
+
+  def test_LSHIFT # '<<'
+    a = @cls[]
+    a << 1
+    assert_equal(@cls[1], a)
+    a << 2 << 3
+    assert_equal(@cls[1, 2, 3], a)
+    a << nil << 'cat'
+    assert_equal(@cls[1, 2, 3, nil, 'cat'], a)
+    a << a
+    assert_equal(@cls[1, 2, 3, nil, 'cat', a], a)
+  end
+
+  def test_CMP # '<=>'
+    assert_equal(0,  @cls[] <=> @cls[])
+    assert_equal(0,  @cls[1] <=> @cls[1])
+    assert_equal(0,  @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'cat'])
+    assert_equal(-1, @cls[] <=> @cls[1])
+    assert_equal(1,  @cls[1] <=> @cls[])
+    assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 'cat'])
+    assert_equal(1,  @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3])
+    assert_equal(-1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'dog'])
+    assert_equal(1,  @cls[1, 2, 3, 'dog'] <=> @cls[1, 2, 3, 'cat'])
+  end
+
+  def test_EQUAL # '=='
+    assert(@cls[] == @cls[])
+    assert(@cls[1] == @cls[1])
+    assert(@cls[1, 1, 2, 2] == @cls[1, 1, 2, 2])
+    assert(@cls[1.0, 1.0, 2.0, 2.0] == @cls[1, 1, 2, 2])
+  end
+
+  def test_VERY_EQUAL # '==='
+    assert(@cls[] === @cls[])
+    assert(@cls[1] === @cls[1])
+    assert(@cls[1, 1, 2, 2] === @cls[1, 1, 2, 2])
+    assert(@cls[1.0, 1.0, 2.0, 2.0] === @cls[1, 1, 2, 2])
+  end
+
+  def test_AREF # '[]'
+    a = @cls[*(1..100).to_a]
+
+    assert_equal(1, a[0])
+    assert_equal(100, a[99])
+    assert_nil(a[100])
+    assert_equal(100, a[-1])
+    assert_equal(99,  a[-2])
+    assert_equal(1,   a[-100])
+    assert_nil(a[-101])
+    assert_nil(a[-101,0])
+    assert_nil(a[-101,1])
+    assert_nil(a[-101,-1])
+    assert_nil(a[10,-1])
+
+    assert_equal(@cls[1],   a[0,1])
+    assert_equal(@cls[100], a[99,1])
+    assert_equal(@cls[],    a[100,1])
+    assert_equal(@cls[100], a[99,100])
+    assert_equal(@cls[100], a[-1,1])
+    assert_equal(@cls[99],  a[-2,1])
+    assert_equal(@cls[],    a[-100,0])
+    assert_equal(@cls[1],   a[-100,1])
+
+    assert_equal(@cls[10, 11, 12], a[9, 3])
+    assert_equal(@cls[10, 11, 12], a[-91, 3])
+
+    assert_equal(@cls[1],   a[0..0])
+    assert_equal(@cls[100], a[99..99])
+    assert_equal(@cls[],    a[100..100])
+    assert_equal(@cls[100], a[99..200])
+    assert_equal(@cls[100], a[-1..-1])
+    assert_equal(@cls[99],  a[-2..-2])
+
+    assert_equal(@cls[10, 11, 12], a[9..11])
+    assert_equal(@cls[10, 11, 12], a[-91..-89])
+    
+    assert_nil(a[10, -3])
+    # Ruby 1.8 feature change:
+    # Array#[size..x] returns [] instead of nil.
+    #assert_nil(a[10..7])
+    assert_equal [], a[10..7]
+
+    assert_raise(TypeError) {a['cat']}
+  end
+
+  def test_ASET # '[]='
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[0] = 0)
+    assert_equal(@cls[0] + @cls[*(1..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[10,10] = 0)
+    assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[-1] = 0)
+    assert_equal(@cls[*(0..98).to_a] + @cls[0], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[-10, 10] = 0)
+    assert_equal(@cls[*(0..89).to_a] + @cls[0], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[0,1000] = 0)
+    assert_equal(@cls[0] , a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(0, a[10..19] = 0)
+    assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
+
+    b = @cls[*%w( a b c )]
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[0,1] = b)
+    assert_equal(b + @cls[*(1..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[10,10] = b)
+    assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[-1, 1] = b)
+    assert_equal(@cls[*(0..98).to_a] + b, a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[-10, 10] = b)
+    assert_equal(@cls[*(0..89).to_a] + b, a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[0,1000] = b)
+    assert_equal(b , a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(b, a[10..19] = b)
+    assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
+
+    # Ruby 1.8 feature change:
+    # assigning nil does not remove elements.
+=begin
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[0,1] = nil)
+    assert_equal(@cls[*(1..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[10,10] = nil)
+    assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[-1, 1] = nil)
+    assert_equal(@cls[*(0..98).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[-10, 10] = nil)
+    assert_equal(@cls[*(0..89).to_a], a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[0,1000] = nil)
+    assert_equal(@cls[] , a)
+
+    a = @cls[*(0..99).to_a]
+    assert_equal(nil, a[10..19] = nil)
+    assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
+=end
+
+    a = @cls[1, 2, 3]
+    a[1, 0] = a
+    assert_equal([1, 1, 2, 3, 2, 3], a)
+
+    a = @cls[1, 2, 3]
+    a[-1, 0] = a
+    assert_equal([1, 2, 1, 2, 3, 3], a)
+  end
+
+  def test_assoc
+    a1 = @cls[*%w( cat feline )]
+    a2 = @cls[*%w( dog canine )]
+    a3 = @cls[*%w( mule asinine )]
+
+    a = @cls[ a1, a2, a3 ]
+
+    assert_equal(a1, a.assoc('cat'))
+    assert_equal(a3, a.assoc('mule'))
+    assert_equal(nil, a.assoc('asinine'))
+    assert_equal(nil, a.assoc('wombat'))
+    assert_equal(nil, a.assoc(1..2))
+  end
+
+  def test_at
+    a = @cls[*(0..99).to_a]
+    assert_equal(0,   a.at(0))
+    assert_equal(10,  a.at(10))
+    assert_equal(99,  a.at(99))
+    assert_equal(nil, a.at(100))
+    assert_equal(99,  a.at(-1))
+    assert_equal(0,  a.at(-100))
+    assert_equal(nil, a.at(-101))
+    assert_raise(TypeError) { a.at('cat') }
+  end
+
+  def test_clear
+    a = @cls[1, 2, 3]
+    b = a.clear
+    assert_equal(@cls[], a)
+    assert_equal(@cls[], b)
+    assert_equal(a.__id__, b.__id__)
+  end
+
+  def test_clone
+    for taint in [ false, true ]
+      for frozen in [ false, true ]
+        a = @cls[*(0..99).to_a]
+        a.taint  if taint
+        a.freeze if frozen
+        b = a.clone
+
+        assert_equal(a, b)
+        assert(a.__id__ != b.__id__)
+        assert_equal(a.frozen?, b.frozen?)
+        assert_equal(a.tainted?, b.tainted?)
+      end
+    end
+  end
+
+  def test_collect
+    a = @cls[ 1, 'cat', 1..1 ]
+    assert_equal([ Fixnum, String, Range], a.collect {|e| e.class} )
+    assert_equal([ 99, 99, 99], a.collect { 99 } )
+
+    assert_equal([], @cls[].collect { 99 })
+
+    assert_equal([1, 2, 3], @cls[1, 2, 3].collect)
+  end
+
+  # also update map!
+  def test_collect!
+    a = @cls[ 1, 'cat', 1..1 ]
+    assert_equal([ Fixnum, String, Range], a.collect! {|e| e.class} )
+    assert_equal([ Fixnum, String, Range], a)
+   
+    a = @cls[ 1, 'cat', 1..1 ]
+    assert_equal([ 99, 99, 99], a.collect! { 99 } )
+    assert_equal([ 99, 99, 99], a)
+
+    a = @cls[ ]
+    assert_equal([], a.collect! { 99 })
+    assert_equal([], a)
+  end
+
+  def test_compact
+    a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact)
+
+    a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact)
+
+    a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact)
+
+    a = @cls[ 1, 2, 3, 4 ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact)
+  end
+
+  def test_compact!
+    a = @cls[ 1, nil, nil, 2, 3, nil, 4 ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact!)
+    assert_equal(@cls[1, 2, 3, 4], a)
+
+    a = @cls[ nil, 1, nil, 2, 3, nil, 4 ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact!)
+    assert_equal(@cls[1, 2, 3, 4], a)
+
+    a = @cls[ 1, nil, nil, 2, 3, nil, 4, nil ]
+    assert_equal(@cls[1, 2, 3, 4], a.compact!)
+    assert_equal(@cls[1, 2, 3, 4], a)
+
+    a = @cls[ 1, 2, 3, 4 ]
+    assert_equal(nil, a.compact!)
+    assert_equal(@cls[1, 2, 3, 4], a)
+  end
+
+  def test_concat
+    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2].concat(@cls[3, 4]))
+    assert_equal(@cls[1, 2, 3, 4],     @cls[].concat(@cls[1, 2, 3, 4]))
+    assert_equal(@cls[1, 2, 3, 4],     @cls[1, 2, 3, 4].concat(@cls[]))
+    assert_equal(@cls[],               @cls[].concat(@cls[]))
+    assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]]))
+    
+    a = @cls[1, 2, 3]
+    a.concat(a)
+    assert_equal([1, 2, 3, 1, 2, 3], a)
+  end
+
+  def test_delete
+    a = @cls[*('cab'..'cat').to_a]
+    assert_equal('cap', a.delete('cap'))
+    assert_equal(@cls[*('cab'..'cao').to_a] + @cls[*('caq'..'cat').to_a], a)
+
+    a = @cls[*('cab'..'cat').to_a]
+    assert_equal('cab', a.delete('cab'))
+    assert_equal(@cls[*('cac'..'cat').to_a], a)
+
+    a = @cls[*('cab'..'cat').to_a]
+    assert_equal('cat', a.delete('cat'))
+    assert_equal(@cls[*('cab'..'cas').to_a], a)
+
+    a = @cls[*('cab'..'cat').to_a]
+    assert_equal(nil, a.delete('cup'))
+    assert_equal(@cls[*('cab'..'cat').to_a], a)
+
+    a = @cls[*('cab'..'cat').to_a]
+    assert_equal(99, a.delete('cup') { 99 } )
+    assert_equal(@cls[*('cab'..'cat').to_a], a)
+  end
+
+  def test_delete_at
+    a = @cls[*(1..5).to_a]
+    assert_equal(3, a.delete_at(2))
+    assert_equal(@cls[1, 2, 4, 5], a)
+
+    a = @cls[*(1..5).to_a]
+    assert_equal(4, a.delete_at(-2))
+    assert_equal(@cls[1, 2, 3, 5], a)
+
+    a = @cls[*(1..5).to_a]
+    assert_equal(nil, a.delete_at(5))
+    assert_equal(@cls[1, 2, 3, 4, 5], a)
+
+    a = @cls[*(1..5).to_a]
+    assert_equal(nil, a.delete_at(-6))
+    assert_equal(@cls[1, 2, 3, 4, 5], a)
+  end
+
+  # also reject!
+  def test_delete_if
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(a, a.delete_if { false })
+    assert_equal(@cls[1, 2, 3, 4, 5], a)
+
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(a, a.delete_if { true })
+    assert_equal(@cls[], a)
+
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(a, a.delete_if { |i| i > 3 })
+    assert_equal(@cls[1, 2, 3], a)
+  end
+
+  def test_dup
+    for taint in [ false, true ]
+      for frozen in [ false, true ]
+        a = @cls[*(0..99).to_a]
+        a.taint  if taint
+        a.freeze if frozen
+        b = a.dup
+
+        assert_equal(a, b)
+        assert(a.__id__ != b.__id__)
+        assert_equal(false, b.frozen?)
+        assert_equal(a.tainted?, b.tainted?)
+      end
+    end
+  end
+
+  def test_each
+    a = @cls[*%w( ant bat cat dog )]
+    i = 0
+    a.each { |e|
+      assert_equal(a[i], e)
+      i += 1
+    }
+    assert_equal(4, i)
+
+    a = @cls[]
+    i = 0
+    a.each { |e|
+      assert_equal(a[i], e)
+      i += 1
+    }
+    assert_equal(0, i)
+
+    assert_equal(a, a.each {})
+  end
+
+  def test_each_index
+    a = @cls[*%w( ant bat cat dog )]
+    i = 0
+    a.each_index { |ind|
+      assert_equal(i, ind)
+      i += 1
+    }
+    assert_equal(4, i)
+
+    a = @cls[]
+    i = 0
+    a.each_index { |ind|
+      assert_equal(i, ind)
+      i += 1
+    }
+    assert_equal(0, i)
+
+    assert_equal(a, a.each_index {})
+  end
+
+  def test_empty?
+    assert(@cls[].empty?)
+    assert(!@cls[1].empty?)
+  end
+
+  def test_eql?
+    assert(@cls[].eql?(@cls[]))
+    assert(@cls[1].eql?(@cls[1]))
+    assert(@cls[1, 1, 2, 2].eql?(@cls[1, 1, 2, 2]))
+    assert(!@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2]))
+  end
+
+  def test_fill
+    assert_equal(@cls[],   @cls[].fill(99))
+    assert_equal(@cls[],   @cls[].fill(99, 0))
+    assert_equal(@cls[99], @cls[].fill(99, 0, 1))
+    assert_equal(@cls[99], @cls[].fill(99, 0..0))
+
+    assert_equal(@cls[99],   @cls[1].fill(99))
+    assert_equal(@cls[99],   @cls[1].fill(99, 0))
+    assert_equal(@cls[99],   @cls[1].fill(99, 0, 1))
+    assert_equal(@cls[99],   @cls[1].fill(99, 0..0))
+
+    assert_equal(@cls[99, 99], @cls[1, 2].fill(99))
+    assert_equal(@cls[99, 99], @cls[1, 2].fill(99, 0))
+    assert_equal(@cls[99, 99], @cls[1, 2].fill(99, nil))
+    assert_equal(@cls[1,  99], @cls[1, 2].fill(99, 1, nil))
+    assert_equal(@cls[99,  2], @cls[1, 2].fill(99, 0, 1))
+    assert_equal(@cls[99,  2], @cls[1, 2].fill(99, 0..0))
+  end
+
+  def test_first
+    assert_equal(3,   @cls[3, 4, 5].first)
+    assert_equal(nil, @cls[].first)
+  end
+
+  def test_flatten
+    a1 = @cls[ 1, 2, 3]
+    a2 = @cls[ 5, 6 ]
+    a3 = @cls[ 4, a2 ]
+    a4 = @cls[ a1, a3 ]
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten)
+    assert_equal(@cls[ a1, a3], a4)
+
+    a5 = @cls[ a1, @cls[], a3 ]
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten)
+    assert_equal(@cls[], @cls[].flatten)
+    assert_equal(@cls[], 
+                 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
+
+    assert_raise(TypeError, "[ruby-dev:31197]") { [[]].flatten("") }
+  end
+
+  def test_flatten!
+    a1 = @cls[ 1, 2, 3]
+    a2 = @cls[ 5, 6 ]
+    a3 = @cls[ 4, a2 ]
+    a4 = @cls[ a1, a3 ]
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4.flatten!)
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a4)
+
+    a5 = @cls[ a1, @cls[], a3 ]
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5.flatten!)
+    assert_equal(@cls[1, 2, 3, 4, 5, 6], a5)
+
+    assert_equal(@cls[], @cls[].flatten)
+    assert_equal(@cls[], 
+                 @cls[@cls[@cls[@cls[],@cls[]],@cls[@cls[]],@cls[]],@cls[@cls[@cls[]]]].flatten)
+  end
+
+  def test_hash
+    a1 = @cls[ 'cat', 'dog' ]
+    a2 = @cls[ 'cat', 'dog' ]
+    a3 = @cls[ 'dog', 'cat' ]
+    assert(a1.hash == a2.hash)
+    assert(a1.hash != a3.hash)
+  end
+
+  def test_include?
+    a = @cls[ 'cat', 99, /a/, @cls[ 1, 2, 3] ]
+    assert(a.include?('cat'))
+    assert(a.include?(99))
+    assert(a.include?(/a/))
+    assert(a.include?([1,2,3]))
+    assert(!a.include?('ca'))
+    assert(!a.include?([1,2]))
+  end
+
+  def test_index
+    a = @cls[ 'cat', 99, /a/, 99, @cls[ 1, 2, 3] ]
+    assert_equal(0, a.index('cat'))
+    assert_equal(1, a.index(99))
+    assert_equal(4, a.index([1,2,3]))
+    assert_nil(a.index('ca'))
+    assert_nil(a.index([1,2]))
+  end
+
+  def test_values_at
+    a = @cls[*('a'..'j').to_a]
+    assert_equal(@cls['a', 'c', 'e'], a.values_at(0, 2, 4))
+    assert_equal(@cls['j', 'h', 'f'], a.values_at(-1, -3, -5))
+    assert_equal(@cls['h', nil, 'a'], a.values_at(-3, 99, 0))
+  end
+
+  def test_join
+    $, = ""
+    a = @cls[]
+    assert_equal("", a.join)
+    assert_equal("", a.join(','))
+
+    $, = ""
+    a = @cls[1, 2]
+    assert_equal("12", a.join)
+    assert_equal("1,2", a.join(','))
+
+    $, = ""
+    a = @cls[1, 2, 3]
+    assert_equal("123", a.join)
+    assert_equal("1,2,3", a.join(','))
+
+    $, = ":"
+    a = @cls[1, 2, 3]
+    assert_equal("1:2:3", a.join)
+    assert_equal("1,2,3", a.join(','))
+
+    $, = ""
+  end
+
+  def test_last
+    assert_equal(nil, @cls[].last)
+    assert_equal(1, @cls[1].last)
+    assert_equal(99, @cls[*(3..99).to_a].last)
+  end
+
+  def test_length
+    assert_equal(0, @cls[].length)
+    assert_equal(1, @cls[1].length)
+    assert_equal(2, @cls[1, nil].length)
+    assert_equal(2, @cls[nil, 1].length)
+    assert_equal(234, @cls[*(0..233).to_a].length)
+  end
+
+  # also update collect!
+  def test_map!
+    a = @cls[ 1, 'cat', 1..1 ]
+    assert_equal(@cls[ Fixnum, String, Range], a.map! {|e| e.class} )
+    assert_equal(@cls[ Fixnum, String, Range], a)
+   
+    a = @cls[ 1, 'cat', 1..1 ]
+    assert_equal(@cls[ 99, 99, 99], a.map! { 99 } )
+    assert_equal(@cls[ 99, 99, 99], a)
+
+    a = @cls[ ]
+    assert_equal(@cls[], a.map! { 99 })
+    assert_equal(@cls[], a)
+  end
+
+  def test_nitems
+    assert_equal(0, @cls[].nitems)
+    assert_equal(1, @cls[1].nitems)
+    assert_equal(1, @cls[1, nil].nitems)
+    assert_equal(1, @cls[nil, 1].nitems)
+    assert_equal(3, @cls[1, nil, nil, 2, nil, 3, nil].nitems)
+  end
+
+  def test_pack
+    a = @cls[*%w( cat wombat x yy)]
+    assert_equal("catwomx  yy ", a.pack("A3A3A3A3"))
+    assert_equal("cat", a.pack("A*"))
+    assert_equal("cwx  yy ", a.pack("A3@1A3@2A3A3"))
+    assert_equal("catwomx\000\000yy\000", a.pack("a3a3a3a3"))
+    assert_equal("cat", a.pack("a*"))
+    assert_equal("ca", a.pack("a2"))
+    assert_equal("cat\000\000", a.pack("a5"))
+
+    assert_equal("\x61",     @cls["01100001"].pack("B8"))
+    assert_equal("\x61",     @cls["01100001"].pack("B*"))
+    assert_equal("\x61",     @cls["0110000100110111"].pack("B8"))
+    assert_equal("\x61\x37", @cls["0110000100110111"].pack("B16"))
+    assert_equal("\x61\x37", @cls["01100001", "00110111"].pack("B8B8"))
+    assert_equal("\x60",     @cls["01100001"].pack("B4"))
+    assert_equal("\x40",     @cls["01100001"].pack("B2"))
+
+    assert_equal("\x86",     @cls["01100001"].pack("b8"))
+    assert_equal("\x86",     @cls["01100001"].pack("b*"))
+    assert_equal("\x86",     @cls["0110000100110111"].pack("b8"))
+    assert_equal("\x86\xec", @cls["0110000100110111"].pack("b16"))
+    assert_equal("\x86\xec", @cls["01100001", "00110111"].pack("b8b8"))
+    assert_equal("\x06",     @cls["01100001"].pack("b4"))
+    assert_equal("\x02",     @cls["01100001"].pack("b2"))
+
+    assert_equal("ABC",      @cls[ 65, 66, 67 ].pack("C3"))
+    assert_equal("\377BC",   @cls[ -1, 66, 67 ].pack("C*"))
+    assert_equal("ABC",      @cls[ 65, 66, 67 ].pack("c3"))
+    assert_equal("\377BC",   @cls[ -1, 66, 67 ].pack("c*"))
+
+    
+    assert_equal("AB\n\x10",  @cls["4142", "0a", "12"].pack("H4H2H1"))
+    assert_equal("AB\n\x02",  @cls["1424", "a0", "21"].pack("h4h2h1"))
+
+    assert_equal("abc=02def=\ncat=\n=01=\n", 
+                 @cls["abc\002def", "cat", "\001"].pack("M9M3M4"))
+
+    assert_equal("aGVsbG8K\n",  @cls["hello\n"].pack("m"))
+    assert_equal(",:&5L;&\\*:&5L;&\\*\n",  @cls["hello\nhello\n"].pack("u"))
+
+    assert_equal("\xc2\xa9B\xe2\x89\xa0", @cls[0xa9, 0x42, 0x2260].pack("U*"))
+
+
+    format = "c2x5CCxsdils_l_a6";
+    # Need the expression in here to force ary[5] to be numeric.  This avoids
+    # test2 failing because ary2 goes str->numeric->str and ary does not.
+    ary = [1, -100, 127, 128, 32767, 987.654321098/100.0,
+      12345, 123456, -32767, -123456, "abcdef"]
+    x    = ary.pack(format)
+    ary2 = x.unpack(format)
+
+    assert_equal(ary.length, ary2.length)
+    assert_equal(ary.join(':'), ary2.join(':'))
+    assert_not_nil(x =~ /def/)
+
+=begin
+    skipping "Not tested:
+        D,d & double-precision float, native format\\
+        E & double-precision float, little-endian byte order\\
+        e & single-precision float, little-endian byte order\\
+        F,f & single-precision float, native format\\
+        G & double-precision float, network (big-endian) byte order\\
+        g & single-precision float, network (big-endian) byte order\\
+        I & unsigned integer\\
+        i & integer\\
+        L & unsigned long\\
+        l & long\\
+
+        N & long, network (big-endian) byte order\\
+        n & short, network (big-endian) byte-order\\
+        P & pointer to a structure (fixed-length string)\\
+        p & pointer to a null-terminated string\\
+        S & unsigned short\\
+        s & short\\
+        V & long, little-endian byte order\\
+        v & short, little-endian byte order\\
+        X & back up a byte\\
+        x & null byte\\
+        Z & ASCII string (null padded, count is width)\\
+"
+=end
+  end
+
+  def test_pop
+    a = @cls[ 'cat', 'dog' ]
+    assert_equal('dog', a.pop)
+    assert_equal(@cls['cat'], a)
+    assert_equal('cat', a.pop)
+    assert_equal(@cls[], a)
+    assert_nil(a.pop)
+    assert_equal(@cls[], a)
+  end
+
+  def test_push
+    a = @cls[1, 2, 3]
+    assert_equal(@cls[1, 2, 3, 4, 5], a.push(4, 5))
+    assert_equal(@cls[1, 2, 3, 4, 5, nil], a.push(nil))
+    # Ruby 1.8 feature:
+    # Array#push accepts any number of arguments.
+    #assert_raise(ArgumentError, "a.push()") { a.push() }
+    a.push
+    assert_equal @cls[1, 2, 3, 4, 5, nil], a
+    a.push 6, 7
+    assert_equal @cls[1, 2, 3, 4, 5, nil, 6, 7], a
+  end
+
+  def test_rassoc
+    a1 = @cls[*%w( cat  feline )]
+    a2 = @cls[*%w( dog  canine )]
+    a3 = @cls[*%w( mule asinine )]
+    a  = @cls[ a1, a2, a3 ]
+
+    assert_equal(a1,  a.rassoc('feline'))
+    assert_equal(a3,  a.rassoc('asinine'))
+    assert_equal(nil, a.rassoc('dog'))
+    assert_equal(nil, a.rassoc('mule'))
+    assert_equal(nil, a.rassoc(1..2))
+  end
+
+  # also delete_if
+  def test_reject!
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(nil, a.reject! { false })
+    assert_equal(@cls[1, 2, 3, 4, 5], a)
+
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(a, a.reject! { true })
+    assert_equal(@cls[], a)
+
+    a = @cls[ 1, 2, 3, 4, 5 ]
+    assert_equal(a, a.reject! { |i| i > 3 })
+    assert_equal(@cls[1, 2, 3], a)
+  end
+
+  def test_replace
+    a = @cls[ 1, 2, 3]
+    a_id = a.__id__
+    assert_equal(@cls[4, 5, 6], a.replace(@cls[4, 5, 6]))
+    assert_equal(@cls[4, 5, 6], a)
+    assert_equal(a_id, a.__id__)
+    assert_equal(@cls[], a.replace(@cls[]))
+  end
+
+  def test_reverse
+    a = @cls[*%w( dog cat bee ant )]
+    assert_equal(@cls[*%w(ant bee cat dog)], a.reverse)
+    assert_equal(@cls[*%w(dog cat bee ant)], a)
+    assert_equal(@cls[], @cls[].reverse)
+  end
+
+  def test_reverse!
+    a = @cls[*%w( dog cat bee ant )]
+    assert_equal(@cls[*%w(ant bee cat dog)], a.reverse!)
+    assert_equal(@cls[*%w(ant bee cat dog)], a)
+    # Ruby 1.8 feature change:
+    # Array#reverse always returns self.
+    #assert_nil(@cls[].reverse!)
+    assert_equal @cls[], @cls[].reverse!
+  end
+
+  def test_reverse_each
+    a = @cls[*%w( dog cat bee ant )]
+    i = a.length
+    a.reverse_each { |e|
+      i -= 1
+      assert_equal(a[i], e)
+    }
+    assert_equal(0, i)
+
+    a = @cls[]
+    i = 0
+    a.reverse_each { |e|
+      assert(false, "Never get here")
+    }
+    assert_equal(0, i)
+  end
+
+  def test_rindex
+    a = @cls[ 'cat', 99, /a/, 99, [ 1, 2, 3] ]
+    assert_equal(0, a.rindex('cat'))
+    assert_equal(3, a.rindex(99))
+    assert_equal(4, a.rindex([1,2,3]))
+    assert_nil(a.rindex('ca'))
+    assert_nil(a.rindex([1,2]))
+  end
+
+  def test_shift
+    a = @cls[ 'cat', 'dog' ]
+    assert_equal('cat', a.shift)
+    assert_equal(@cls['dog'], a)
+    assert_equal('dog', a.shift)
+    assert_equal(@cls[], a)
+    assert_nil(a.shift)
+    assert_equal(@cls[], a)
+  end
+
+  def test_size
+    assert_equal(0,   @cls[].size)
+    assert_equal(1,   @cls[1].size)
+    assert_equal(100, @cls[*(0..99).to_a].size)
+  end
+
   def test_slice
     a = @cls[*(1..100).to_a]
 
@@ -229,4 +1087,163 @@
     assert_equal(@cls[1, 2, 3, 4, 5], a)
   end
 
+  def test_sort
+    a = @cls[ 4, 1, 2, 3 ]
+    assert_equal(@cls[1, 2, 3, 4], a.sort)
+    assert_equal(@cls[4, 1, 2, 3], a)
+
+    assert_equal(@cls[4, 3, 2, 1], a.sort { |x, y| y <=> x} )
+    assert_equal(@cls[4, 1, 2, 3], a)
+
+    a.fill(1)
+    assert_equal(@cls[1, 1, 1, 1], a.sort)
+    
+    assert_equal(@cls[], @cls[].sort)
+  end
+
+  def test_sort!
+    a = @cls[ 4, 1, 2, 3 ]
+    assert_equal(@cls[1, 2, 3, 4], a.sort!)
+    assert_equal(@cls[1, 2, 3, 4], a)
+
+    assert_equal(@cls[4, 3, 2, 1], a.sort! { |x, y| y <=> x} )
+    assert_equal(@cls[4, 3, 2, 1], a)
+
+    a.fill(1)
+    assert_equal(@cls[1, 1, 1, 1], a.sort!)
+
+    assert_equal(@cls[1], @cls[1].sort!)
+    assert_equal(@cls[], @cls[].sort!)
+  end
+
+  def test_to_a
+    a = @cls[ 1, 2, 3 ]
+    a_id = a.__id__
+    assert_equal(a, a.to_a)
+    assert_equal(a_id, a.to_a.__id__)
+  end
+
+  def test_to_ary
+    a = [ 1, 2, 3 ]
+    b = @cls[*a]
+
+    a_id = a.__id__
+    assert_equal(a, b.to_ary)
+    if (@cls == Array)
+      assert_equal(a_id, a.to_ary.__id__)
+    end
+  end
+
+  def test_to_s
+    $, = ""
+    a = @cls[]
+    assert_equal("", a.to_s)
+
+    $, = ""
+    a = @cls[1, 2]
+    assert_equal("12", a.to_s)
+
+    $, = ""
+    a = @cls[1, 2, 3]
+    assert_equal("123", a.to_s)
+
+    $, = ":"
+    a = @cls[1, 2, 3]
+    assert_equal("1:2:3", a.to_s)
+
+    $, = ""
+  end
+
+  def test_uniq
+    a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
+    b = a.dup
+    assert_equal(@cls[1, 2, 3, 4, nil], a.uniq)
+    assert_equal(b, a)
+
+    assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq)
+  end
+
+  def test_uniq!
+    a = @cls[ 1, 2, 3, 2, 1, 2, 3, 4, nil ]
+    assert_equal(@cls[1, 2, 3, 4, nil], a.uniq!)
+    assert_equal(@cls[1, 2, 3, 4, nil], a)
+
+    assert_nil(@cls[1, 2, 3].uniq!)
+  end
+
+  def test_unshift
+    a = @cls[]
+    assert_equal(@cls['cat'], a.unshift('cat'))
+    assert_equal(@cls['dog', 'cat'], a.unshift('dog'))
+    assert_equal(@cls[nil, 'dog', 'cat'], a.unshift(nil))
+    assert_equal(@cls[@cls[1,2], nil, 'dog', 'cat'], a.unshift(@cls[1, 2]))
+  end
+
+  def test_OR # '|'
+    assert_equal(@cls[],  @cls[]  | @cls[])
+    assert_equal(@cls[1], @cls[1] | @cls[])
+    assert_equal(@cls[1], @cls[]  | @cls[1])
+    assert_equal(@cls[1], @cls[1] | @cls[1])
+
+    assert_equal(@cls[1,2], @cls[1] | @cls[2])
+    assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2])
+    assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
+  end
+
+ def test_combination
+    assert_equal(@cls[[]], @cls[1,2,3,4].combination(0).to_a)
+    assert_equal(@cls[[1],[2],[3],[4]], @cls[1,2,3,4].combination(1).to_a)
+    assert_equal(@cls[[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]], @cls[1,2,3,4].combination(2).to_a)
+    assert_equal(@cls[[1,2,3],[1,2,4],[1,3,4],[2,3,4]], @cls[1,2,3,4].combination(3).to_a)
+    assert_equal(@cls[[1,2,3,4]], @cls[1,2,3,4].combination(4).to_a)
+    assert_equal(@cls[], @cls[1,2,3,4].combination(5).to_a)
+  end
+
+  def test_product
+    assert_equal(@cls[[1,4],[1,5],[2,4],[2,5],[3,4],[3,5]],
+                 @cls[1,2,3].product([4,5]))
+    assert_equal(@cls[[1,1],[1,2],[2,1],[2,2]], @cls[1,2].product([1,2]))
+
+    assert_equal(@cls[[1,3,5],[1,3,6],[1,4,5],[1,4,6],
+                   [2,3,5],[2,3,6],[2,4,5],[2,4,6]], 
+                 @cls[1,2].product([3,4],[5,6]))
+    assert_equal(@cls[[1],[2]], @cls[1,2].product)
+    assert_equal(@cls[], @cls[1,2].product([]))
+  end
+
+  def test_permutation
+    a = @cls[1,2,3]
+    assert_equal(@cls[[]], a.permutation(0).to_a)
+    assert_equal(@cls[[1],[2],[3]], a.permutation(1).to_a.sort)
+    assert_equal(@cls[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
+                 a.permutation(2).to_a.sort)
+    assert_equal(@cls[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
+                 a.permutation(3).sort.to_a)
+    assert_equal(@cls[], a.permutation(4).to_a)
+    assert_equal(@cls[], a.permutation(-1).to_a)
+    assert_equal("abcde".each_char.to_a.permutation(5).sort,
+                 "edcba".each_char.to_a.permutation(5).sort)
+    assert_equal(@cls[].permutation(0).to_a, @cls[[]])
+
+  end
+
+  def test_take
+    assert_equal([1,2,3], [1,2,3,4,5,0].take(3))
+    assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].take(-1) }
+    assert_equal([1,2], [1,2].take(1000000000), '[ruby-dev:34123]')
+  end
+
+  def test_take_while
+    assert_equal([1,2], [1,2,3,4,5,0].take_while {|i| i < 3 })
+  end
+
+  def test_drop
+    assert_equal([4,5,0], [1,2,3,4,5,0].drop(3))
+    assert_raise(ArgumentError, '[ruby-dev:34123]') { [1,2].drop(-1) }
+    assert_equal([], [1,2].drop(1000000000), '[ruby-dev:34123]')
+  end
+
+  def test_drop_while
+    assert_equal([3,4,5,0], [1,2,3,4,5,0].drop_while {|i| i < 3 })
+  end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]