feat(Vector)

This commit is contained in:
2025-07-23 10:36:50 +03:00
parent 83c491ff6f
commit aca4539ce4
21 changed files with 1103 additions and 1533 deletions

View File

@ -1,151 +1,11 @@
import { expect, test } from 'vitest'
import { List, ListIterator } from '../src/List'
import { SequenceContainer } from '../src/containers/Sequence'
import { BidirectionalIterator } from '../src/iterators/Bidirectional'
import { describe, expect, test } from "vitest"
import { SequenceContainer } from "../src/containers/Sequence"
import { List, ListIterator } from "../src/List"
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(() => new List().back).toThrow()
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('resize', () => {
{
const list = List.from([1, 2, 3])
list.resize(1)
expect(listEquArray(list, [1])).toBeTruthy()
}
{
const list = List.from([1])
list.resize(3, 2)
expect(listEquArray(list, [1, 2, 2])).toBeTruthy()
}
})
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('pushBack, pushFront, popBack, popFront, back, front', () => {
const list = List.from([1, 2, 3])
list.popFront()
list.pushFront(5)
list.popBack()
list.popBack()
list.pushBack(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 ]')
expect(List.from([0n]).toString()).toEqual('[ 0 ]')
describe(List.name, () => {
test('@concepts', () => {
expect(SequenceContainer.is(List.prototype, true)).toBeTruthy()
expect(BidirectionalIterator.is(ListIterator.prototype, true)).toBeTruthy()
})
})

25
test/Vector.test.js Normal file
View File

@ -0,0 +1,25 @@
import { describe, expect, test } from "vitest";
import { RandomAccessIterator, SequenceContainer, Vector, VectorIterator } from "../src";
describe(Vector.name, () => {
test('@concepts', () => {
expect(SequenceContainer.is(Vector.prototype, true)).toBeTruthy()
expect(RandomAccessIterator.is(VectorIterator.prototype, true)).toBeTruthy()
})
test('get, set', () => {
const vec = new Vector(0, 2)
expect(vec.get(1)).toEqual(0)
vec.set(1, 2)
expect(vec.get(1)).toEqual(2)
expect(() => vec.get(2)).toThrow()
})
})
describe(VectorIterator.name, () => {
test('sub', () => {
const vector = Vector.from([1, 2, 3])
expect(vector.begin().add(2).value).toEqual(3)
expect(vector.begin().add(2).sub(vector.begin())).toEqual(2)
})
})

View File

@ -0,0 +1,176 @@
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())
})
})
}