177 lines
4.8 KiB
JavaScript
177 lines
4.8 KiB
JavaScript
import { describe, expect, test } from 'vitest'
|
|
import { List } from '../../src/List'
|
|
import { Vector } from '../../src/Vector'
|
|
|
|
function seqEquArray(seq, arr) {
|
|
if (seq.size() != Array.from(arr).length) {
|
|
return false
|
|
}
|
|
let it = seq.begin(), it_end = seq.end(), idx = 0
|
|
while (it.neq(it_end)) {
|
|
if (arr[idx] !== it.value) {
|
|
return false
|
|
}
|
|
idx++
|
|
it.inc()
|
|
}
|
|
return true
|
|
}
|
|
|
|
for (const Type of [List, Vector]) {
|
|
describe(Type.name, () => {
|
|
test('from', () => {
|
|
expect(seqEquArray(Type.from([]), [])).toBeTruthy()
|
|
expect(seqEquArray(Type.from([1, 2, 3]), [1, 2, 3])).toBeTruthy()
|
|
{
|
|
const seq = Type.from([1, 2, 3])
|
|
const seq2 = Type.from(seq)
|
|
seq.clear()
|
|
expect(seqEquArray(seq2, [1, 2, 3]))
|
|
}
|
|
{
|
|
const iterable = {
|
|
[Symbol.iterator]: () => {
|
|
let i = 2;
|
|
return {
|
|
next: () => {
|
|
return {
|
|
done: i == 0,
|
|
value: i--,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
expect(seqEquArray(Type.from(iterable), [2, 1])).toBeTruthy()
|
|
}
|
|
})
|
|
|
|
test('copy, clone', () => {
|
|
const seq = Type.from([1, 2, 3])
|
|
const a = new Type(), b = seq.clone()
|
|
a.copy(seq)
|
|
expect(a.size()).toEqual(3)
|
|
expect(b.size()).toEqual(3)
|
|
seq.clear()
|
|
expect(a.size()).toEqual(3)
|
|
expect(b.size()).toEqual(3)
|
|
})
|
|
|
|
test('move, empty', () => {
|
|
const a = Type.from([1, 2, 3])
|
|
const b = new Type()
|
|
b.move(a)
|
|
expect(b.size()).toEqual(3)
|
|
})
|
|
|
|
test('constructor, assign', () => {
|
|
expect(new Type().size()).toEqual(0)
|
|
expect(() => new Type().back).toThrow()
|
|
expect(() => new Type().front).toThrow()
|
|
expect(seqEquArray(new Type(null), [null])).toBeTruthy()
|
|
expect(seqEquArray(new Type(null, 2), [null, null])).toBeTruthy()
|
|
{
|
|
const seq = Type.from([1, 2, 3])
|
|
expect(seqEquArray(new Type(seq.begin().inc(), seq.end().dec()), [2])).toBeTruthy()
|
|
seq.assign(10)
|
|
expect(seq.size()).toEqual(1)
|
|
}
|
|
})
|
|
|
|
test('resize', () => {
|
|
{
|
|
const seq = Type.from([1, 2, 3])
|
|
seq.resize(1)
|
|
expect(seqEquArray(seq, [1])).toBeTruthy()
|
|
}
|
|
{
|
|
const seq = Type.from([1])
|
|
seq.resize(3, 2)
|
|
expect(seqEquArray(seq, [1, 2, 2])).toBeTruthy()
|
|
}
|
|
})
|
|
|
|
test('insert, clone', () => {
|
|
{
|
|
const seq = Type.from([1, 2])
|
|
expect(() => seq.insert(seq.begin(), undefined)).toThrow()
|
|
}
|
|
{
|
|
const seq = Type.from([1, 2])
|
|
const it = seq.insert(seq.begin().inc(), null, 2)
|
|
expect(seqEquArray(seq, [1, null, null, 2])).toBeTruthy()
|
|
expect(seq.size()).toEqual(4)
|
|
expect(it.eq(seq.begin().inc())).toBeTruthy()
|
|
}
|
|
{
|
|
const a = Type.from([1, 4])
|
|
const b = Type.from([2, 3])
|
|
const it = a.insert(a.begin().inc(), b.begin(), b.end())
|
|
expect(seqEquArray(a, [1, 2, 3, 4])).toBeTruthy()
|
|
expect(a.size()).toEqual(4)
|
|
expect(it.eq(a.begin().inc())).toBeTruthy()
|
|
}
|
|
{
|
|
const seq = Type.from([1, 2])
|
|
expect(() => seq.insert()).toThrowError()
|
|
const it = seq.insert(seq.begin(), 0, 0)
|
|
expect(it.eq(seq.begin())).toBeTruthy()
|
|
}
|
|
})
|
|
|
|
test('erase, clear', () => {
|
|
{
|
|
const seq = Type.from([1, 2, 3, 4])
|
|
const it = seq.erase(seq.begin(), seq.end().dec().dec())
|
|
expect(seqEquArray(seq, [3, 4])).toBeTruthy()
|
|
expect(seq.size()).toEqual(2)
|
|
expect(it.eq(seq.begin())).toBeTruthy()
|
|
}
|
|
{
|
|
const seq = Type.from([1, 2, 3])
|
|
const it = seq.erase(seq.begin().inc())
|
|
expect(seqEquArray(seq, [1, 3])).toBeTruthy()
|
|
expect(seq.size()).toEqual(2)
|
|
expect(it.deref()).toEqual(seq.begin().inc().deref())
|
|
}
|
|
{
|
|
const seq = Type.from([1, 2])
|
|
seq.clear()
|
|
expect(seq.size()).toEqual(0)
|
|
}
|
|
})
|
|
|
|
test('pushBack, pushFront, popBack, popFront, back, front', () => {
|
|
const seq = Type.from([1, 2, 3])
|
|
seq.popFront()
|
|
seq.pushFront(5)
|
|
seq.popBack()
|
|
seq.popBack()
|
|
seq.pushBack(10)
|
|
expect(seqEquArray(seq, [5, 10])).toBeTruthy()
|
|
expect(seq.front).toEqual(5)
|
|
expect(seq.back).toEqual(10)
|
|
seq.front = 1
|
|
seq.back = 2
|
|
expect(seq.front).toEqual(1)
|
|
expect(seq.back).toEqual(2)
|
|
})
|
|
|
|
test('toJSON, toString', () => {
|
|
expect(Type.from([1, 2]).toJSON()).toEqual('[ 1, 2 ]')
|
|
expect(Type.from([0n]).toString()).toEqual('[ 0 ]')
|
|
})
|
|
})
|
|
|
|
describe(Type.name + 'Iterator', () => {
|
|
test('copy', () => {
|
|
const seq = Type.from([1, 2, 3])
|
|
const it1 = seq.begin()
|
|
const it2 = seq.end()
|
|
it2.copy(it1)
|
|
it1.inc()
|
|
expect(it2.deref()).toEqual(seq.begin().deref())
|
|
})
|
|
})
|
|
}
|