136 lines
3.8 KiB
JavaScript
136 lines
3.8 KiB
JavaScript
import { expect, test } from 'vitest'
|
|
import { List, ListIterator } from '../src/List'
|
|
import { SequenceContainer } from '../src/containers/Sequence'
|
|
import { BidirectionalIterator } from '../src/iterators/Bidirectional'
|
|
|
|
function listEquArray(list, arr) {
|
|
if (list.size() != Array.from(arr).length) {
|
|
return false
|
|
}
|
|
let it = list.begin(), it_end = list.end(), idx = 0
|
|
while (it.neq(it_end)) {
|
|
if (arr[idx] !== it.value) {
|
|
return false
|
|
}
|
|
idx++
|
|
it.inc()
|
|
}
|
|
return true
|
|
}
|
|
|
|
test('@concepts', () => {
|
|
expect(SequenceContainer.is(List.prototype, true)).toBeTruthy()
|
|
expect(BidirectionalIterator.is(ListIterator.prototype, true)).toBeTruthy()
|
|
})
|
|
|
|
test('from', () => {
|
|
expect(listEquArray(List.from([]), [])).toBeTruthy()
|
|
expect(listEquArray(List.from([1, 2, 3]), [1, 2, 3])).toBeTruthy()
|
|
})
|
|
|
|
test('copy, clone', () => {
|
|
const list = List.from([1, 2, 3])
|
|
const a = new List(), b = list.clone()
|
|
a.copy(list)
|
|
expect(a.size()).toEqual(3)
|
|
expect(b.size()).toEqual(3)
|
|
list.clear()
|
|
expect(a.size()).toEqual(3)
|
|
expect(b.size()).toEqual(3)
|
|
})
|
|
|
|
test('move, empty', () => {
|
|
const a = List.from([1, 2, 3])
|
|
const b = new List()
|
|
b.move(a)
|
|
expect(a.empty()).toBeTruthy()
|
|
expect(b.size()).toEqual(3)
|
|
})
|
|
|
|
test('constructor, assign', () => {
|
|
expect(new List().size()).toEqual(0)
|
|
expect(listEquArray(new List(null), [null])).toBeTruthy()
|
|
expect(listEquArray(new List(null, 2), [null, null])).toBeTruthy()
|
|
{
|
|
const list = List.from([1, 2, 3])
|
|
expect(listEquArray(new List(list.begin().inc(), list.end().dec()), [2])).toBeTruthy()
|
|
list.assign(10)
|
|
expect(list.size()).toEqual(1)
|
|
}
|
|
})
|
|
|
|
test('insert, clone', () => {
|
|
{
|
|
const list = List.from([1, 2])
|
|
const it = list.insert(list.begin().inc(), null, 2)
|
|
expect(listEquArray(list, [1, null, null, 2])).toBeTruthy()
|
|
expect(list.size()).toEqual(4)
|
|
expect(it.eq(list.begin().inc())).toBeTruthy()
|
|
}
|
|
{
|
|
const a = List.from([1, 4])
|
|
const b = List.from([2, 3])
|
|
const it = a.insert(a.begin().inc(), b.begin(), b.end())
|
|
expect(listEquArray(a, [1, 2, 3, 4])).toBeTruthy()
|
|
expect(a.size()).toEqual(4)
|
|
expect(it.eq(a.begin().inc())).toBeTruthy()
|
|
}
|
|
{
|
|
const list = List.from([1, 2])
|
|
expect(() => list.insert()).toThrowError()
|
|
const it = list.insert(list.begin(), 0, 0)
|
|
expect(it.eq(list.begin())).toBeTruthy()
|
|
}
|
|
{
|
|
const a = List.from([1, 2])
|
|
const b = a.clone()
|
|
expect(listEquArray(b, [1, 2])).toBeTruthy()
|
|
expect(b.size()).toEqual(2)
|
|
expect(b.begin().deref().prev).toEqual(undefined)
|
|
expect(b.begin().deref().next).toEqual(b.end().deref().prev)
|
|
expect(b.end().deref().prev).toEqual(b.begin().deref().next)
|
|
expect(b.end().deref().next).toEqual(undefined)
|
|
}
|
|
})
|
|
|
|
test('erase, clear', () => {
|
|
{
|
|
const list = List.from([1, 2, 3, 4])
|
|
const it = list.erase(list.begin(), list.end().dec().dec())
|
|
expect(listEquArray(list, [3, 4])).toBeTruthy()
|
|
expect(list.size()).toEqual(2)
|
|
expect(it.eq(list.begin())).toBeTruthy()
|
|
}
|
|
{
|
|
const list = List.from([1, 2, 3])
|
|
const it = list.erase(list.begin().inc())
|
|
expect(listEquArray(list, [1, 3])).toBeTruthy()
|
|
expect(list.size()).toEqual(2)
|
|
expect(it.deref()).toEqual(list.begin().inc().deref())
|
|
}
|
|
{
|
|
const list = List.from([1, 2])
|
|
list.clear()
|
|
expect(list.size()).toEqual(0)
|
|
}
|
|
})
|
|
|
|
test('push_back, push_front, pop_back, pop_front, back, front', () => {
|
|
const list = List.from([1, 2, 3])
|
|
list.pop_front()
|
|
list.push_front(5)
|
|
list.pop_back()
|
|
list.pop_back()
|
|
list.push_back(10)
|
|
expect(listEquArray(list, [5, 10])).toBeTruthy()
|
|
expect(list.front).toEqual(5)
|
|
expect(list.back).toEqual(10)
|
|
list.front = 1
|
|
list.back = 2
|
|
expect(list.front).toEqual(1)
|
|
expect(list.back).toEqual(2)
|
|
})
|
|
|
|
test('toJSON, toString', () => {
|
|
expect(List.from([1, 2]).toJSON()).toEqual('[ 1, 2 ]')
|
|
}) |