///|
test {
let _set1 : @sorted_set.SortedSet[Int] = @sorted_set.new()
let _set2 = @sorted_set.singleton(1)
let _set4 = @sorted_set.SortedSet([1])
let _set5 = @sorted_set.SortedSet([1])
}///|
test {
let set = @sorted_set.SortedSet([3, 2, 1])
@test.assert_eq(set.to_array(), [1, 2, 3])
}///|
test {
let set = @sorted_set.SortedSet([1, 2, 3, 4])
@test.assert_eq(set.add(5).to_array(), [1, 2, 3, 4, 5])
}///|
test {
let set = @sorted_set.SortedSet([3, 8, 1])
@test.assert_eq(set.remove(8).to_array(), [1, 3])
}///|
test {
let set = @sorted_set.SortedSet([1, 2, 3, 4])
@test.assert_eq(set.contains(1), true)
@test.assert_eq(set.contains(5), false)
}///|
test {
let set = @sorted_set.SortedSet([1, 2, 3, 4])
@test.assert_eq(set.min(), 1)
@test.assert_eq(set.max(), 4)
@test.assert_eq(set.min_option(), Some(1))
@test.assert_eq(set.max_option(), Some(4))
}///|
test {
let (left, present, right) = @sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).split(
5,
)
@test.assert_eq(present, true)
@test.assert_eq(left.to_array(), [1, 2, 3, 4])
@test.assert_eq(right.to_array(), [6, 7, 8, 9])
}///|
test {
let set1 = @sorted_set.SortedSet([3, 4, 5])
let set2 = @sorted_set.SortedSet([4, 5, 6])
@test.assert_eq(set1.union(set2).to_array(), [3, 4, 5, 6])
@test.assert_eq(set1.intersection(set2).to_array(), [4, 5])
}///|
test {
let set1 = @sorted_set.SortedSet([1, 2, 3])
let set2 = @sorted_set.SortedSet([4, 5, 1])
@test.assert_eq(set1.difference(set2).to_array(), [2, 3])
}///|
test {
let set = @sorted_set.SortedSet([1, 2, 3, 4, 5, 6])
@test.assert_eq(set.filter(v => v % 2 == 0).to_array(), [2, 4, 6])
}///|
test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).subset(
SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]),
),
true,
)
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).disjoint(SortedSet([4, 5, 6])),
true,
)
}///|
test {
let arr = []
@sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).each(v => arr.push(v))
@test.assert_eq(arr, [1, 2, 3, 4, 5, 6, 7, 8, 9])
let val = @sorted_set.SortedSet([1, 2, 3, 4, 5]).fold(init=0, (acc, x) => {
acc + x
})
@test.assert_eq(val, 15)
let set = @sorted_set.SortedSet([1, 2, 3])
@test.assert_eq(set.map(x => x * 2).to_array(), [2, 4, 6])
}///|
test {
let set = @sorted_set.SortedSet([1, 2, 3, 4, 5])
let arr = []
set.rev_iter().each(v => arr.push(v))
@test.assert_eq(arr, [5, 4, 3, 2, 1])
}///|
test {
@test.assert_eq(@sorted_set.SortedSet([2, 4, 6]).all(v => v % 2 == 0), true)
@test.assert_eq(@sorted_set.SortedSet([1, 4, 3]).any(v => v % 2 == 0), true)
}///|
test {
let set1 : @sorted_set.SortedSet[Int] = SortedSet([])
@test.assert_eq(set1.is_empty(), true)
let set2 = @sorted_set.SortedSet([1])
@test.assert_eq(set2.is_empty(), false)
}type SortedSet[A]test {
@test.assert_eq(@sorted_set.SortedSet([3, 1, 2, 3]).to_array(), [1, 2, 3])
}test {
@test.assert_eq(
@sorted_set.SortedSet([6, 3, 8, 1]).add(5),
SortedSet([1, 3, 5, 6, 8]),
)
}test {
@test.assert_eq(@sorted_set.SortedSet([2, 4, 6]).all(v => v % 2 == 0), true)
}test {
@test.assert_eq(@sorted_set.SortedSet([1, 4, 3]).any(v => v % 2 == 0), true)
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).difference(SortedSet([4, 5, 1])),
SortedSet([2, 3]),
)
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).disjoint(SortedSet([4, 5, 6])),
true,
)
}test {
let arr = []
@sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).each(x => arr.push(x))
@test.assert_eq(arr, [1, 2, 3, 4, 5, 6, 7, 8, 9])
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3, 4, 5, 6]).filter(v => v % 2 == 0),
SortedSet([2, 4, 6]),
)
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3, 4, 5]).fold(init=0, (acc, x) => acc + x),
15,
)
}#deprecated("Use @immut/sorted_set.SortedSet([...]) instead")
#as_free_fn(of, deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
#alias(of, deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
#as_free_fn(deprecated="Use @immut/sorted_set.SortedSet([...]) instead")
fn[A : Compare + Eq] SortedSet::from_array(array : ArrayView[A]) -> SortedSet[A]test {
@test.assert_eq(
@sorted_set.SortedSet([3, 4, 5]).intersection(SortedSet([4, 5, 6])),
SortedSet([4, 5]),
)
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).map(x => x * 2),
SortedSet([2, 4, 6]),
)
}test {
@test.assert_eq(@sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).max(), 9)
}test {
@test.assert_eq(@sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).min(), 1)
}test {
let set = @sorted_set.SortedSet([1, 2, 3, 4, 5])
let result = []
for v in set.range(low=2, high=4) {
result.push(v)
}
@test.assert_eq(result, [2, 3, 4])
}test {
@test.assert_eq(@sorted_set.SortedSet([3, 8, 1]).remove(8), SortedSet([1, 3]))
}test {
@test.assert_eq(
@sorted_set.SortedSet([3, 4, 5]).remove_min(),
SortedSet([4, 5]),
)
} let set = @sorted_set.SortedSet([1, 2, 3, 4, 5])
let result = set.rev_iter().collect()
@test.assert_eq(result, [5, 4, 3, 2, 1])test {
let (left, present, right) = @sorted_set.SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]).split(
5,
)
inspect(present, content="true")
@test.assert_eq(left, SortedSet([1, 2, 3, 4]))
@test.assert_eq(right, SortedSet([6, 7, 8, 9]))
}test {
@test.assert_eq(
@sorted_set.SortedSet([1, 2, 3]).subset(
SortedSet([7, 2, 9, 4, 5, 6, 3, 8, 1]),
),
true,
)
}test {
let set1 = @sorted_set.SortedSet([1, 2, 3, 4])
let set2 = @sorted_set.SortedSet([3, 4, 5, 6])
@debug.debug_inspect(
set1.symmetric_difference(set2),
content=(
#|<SortedSet: [1, 2, 5, 6]>
),
)
}Install
Installed by default