}
+func (p *Array) Do(f func(elem Element)) {
+ for i := 0; i < len(p.a); i++ {
+ f(p.a[i]) // not too safe if f changes the Array
+ }
+}
+
+
// Convenience wrappers
func (p *Array) Push(x Element) {
verify_pattern(t, a, 8, 1000, 2);
}
-
func TestSorting(t *testing.T) {
const n = 100;
a := array.NewIntArray(n);
}
if sort.IsSorted(a) { t.Error("not sorted") }
}
+
+
+func TestDo(t *testing.T) {
+ const n = 25;
+ const salt = 17;
+ a := array.NewIntArray(n);
+ for i := 0; i < n; i++ {
+ a.Set(i, salt * i);
+ }
+ count := 0;
+ a.Do(
+ func(e array.Element) {
+ i := e.(int);
+ if i != count*salt {
+ t.Error("value at", count, "should be", count*salt, "not", i)
+ }
+ count++;
+ }
+ );
+ if count != n {
+ t.Error("should visit", n, "values; did visit", count)
+ }
+}