1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
import {
Vector,
LinkList,
Deque,
OrderedSet,
OrderedMap,
Container,
ContainerIterator,
VectorIterator,
LinkListIterator,
DequeIterator,
OrderedSetIterator,
OrderedMapIterator,
SequentialContainer,
TreeContainer
} from '@/index';
let arr: number[] = [];
const testNum = 10000;
for (let i = 0; i < testNum; ++i) {
arr.push(Math.floor(Math.random() * testNum));
}
arr = Array.from(new Set(arr));
arr.sort((x, y) => x - y);
const containerArr: Container<unknown>[] = [
new Vector(arr),
new LinkList(arr),
new Deque(arr),
new OrderedSet(arr),
new OrderedMap(arr.map((element, index) => [index, element]))
];
describe('iterator test', () => {
test('normal iterator next function test', () => {
for (const container of containerArr) {
let index = 0;
for (let it = container.begin() as ContainerIterator<unknown>;
!it.equals(container.end() as ContainerIterator<unknown>);
it = it.next()) {
if (container instanceof OrderedMap) {
expect((it as ContainerIterator<[number, number]>).pointer[1])
.toEqual(arr[index++]);
} else {
expect(it.pointer).toEqual(arr[index++]);
}
}
}
});
test('normal iterator pre function test', () => {
for (const container of containerArr) {
let index = arr.length - 1;
for (let it = container.end().pre() as ContainerIterator<unknown>;
!it.equals(container.begin() as ContainerIterator<unknown>);
it = it.pre()) {
if (container instanceof OrderedMap) {
expect((it as ContainerIterator<[number, number]>).pointer[1])
.toEqual(arr[index--]);
} else {
expect(it.pointer).toEqual(arr[index--]);
}
}
}
});
test('reverse iterator next function test', () => {
for (const container of containerArr) {
let index = arr.length - 1;
for (let it = container.rBegin() as ContainerIterator<unknown>;
!it.equals(container.rEnd() as ContainerIterator<unknown>);
it = it.next()) {
if (container instanceof OrderedMap) {
expect((it as ContainerIterator<[number, number]>).pointer[1])
.toEqual(arr[index--]);
} else {
expect(it.pointer).toEqual(arr[index--]);
}
}
}
});
test('reverse iterator pre function test', () => {
for (const container of containerArr) {
let index = 0;
for (let it = container.rEnd().pre() as ContainerIterator<unknown>;
!it.equals(container.rBegin() as ContainerIterator<unknown>);
it = it.pre()) {
if (container instanceof OrderedMap) {
expect((it as ContainerIterator<[number, number]>).pointer[1])
.toEqual(arr[index++]);
} else {
expect(it.pointer).toEqual(arr[index++]);
}
}
}
});
for (const container of containerArr) {
test('normal iterator next run time error test', () => {
expect(() => container.end().next()).toThrowError(RangeError);
});
test('normal iterator pre run time error test', () => {
expect(() => container.begin().pre()).toThrowError(RangeError);
});
test('reverse iterator next run time error test', () => {
expect(() => container.rEnd().next()).toThrowError(RangeError);
});
test('reverse iterator pre run time error test', () => {
expect(() => container.rBegin().pre()).toThrowError(RangeError);
});
}
test('copy test', () => {
for (const container of containerArr) {
const iter = container.begin() as ContainerIterator<unknown>;
const copy = iter.copy() as ContainerIterator<unknown>;
iter.next();
expect(iter.equals(copy)).toBe(false);
copy.next();
expect(iter.equals(copy)).toBe(true);
}
for (const container of containerArr) {
const iter = container.end() as ContainerIterator<unknown>;
const copy = iter.copy() as ContainerIterator<unknown>;
iter.pre();
expect(iter.equals(copy)).toBe(false);
copy.pre();
expect(iter.equals(copy)).toBe(true);
}
for (const container of containerArr) {
const iter = container.rBegin() as ContainerIterator<unknown>;
const copy = iter.copy() as ContainerIterator<unknown>;
iter.next();
expect(iter.equals(copy)).toBe(false);
copy.next();
expect(iter.equals(copy)).toBe(true);
}
for (const container of containerArr) {
const iter = container.rEnd() as ContainerIterator<unknown>;
const copy = iter.copy() as ContainerIterator<unknown>;
iter.pre();
expect(iter.equals(copy)).toBe(false);
copy.pre();
expect(iter.equals(copy)).toBe(true);
}
});
test('export test', () => {
expect(() => {
// eslint-disable-next-line no-new
new VectorIterator<number>(0,
function () {
return 0;
}, function () {
return 0;
}, function () {
return 0;
});
// @ts-ignore
// eslint-disable-next-line no-new
new Container();
// @ts-ignore
// eslint-disable-next-line no-new
new ContainerIterator();
// @ts-ignore
// eslint-disable-next-line no-new
new SequentialContainer();
// @ts-ignore
// eslint-disable-next-line no-new
new TreeContainer();
// @ts-ignore
// eslint-disable-next-line no-new
new LinkListIterator<number>(containerArr[0].begin().node);
// eslint-disable-next-line no-new
new DequeIterator<number>(0,
function () {
return 0;
}, function () {
return 0;
}, function () {
return 0;
});
// @ts-ignore
// eslint-disable-next-line no-new
new OrderedSetIterator<number>(containerArr[3].begin().node, containerArr[3].header);
// @ts-ignore
// eslint-disable-next-line no-new
new OrderedMapIterator<number, number>(containerArr[4].begin().node, containerArr[4].header);
}).not.toThrowError(Error);
});
});
|