Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wewei committed Sep 10, 2024
1 parent d035dd7 commit e5519a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
56 changes: 21 additions & 35 deletions packages/@ot-doc/document/src/lib/array-document.spec.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
import { describeDocumentMeta, DocumentTestCases } from "../util/test-utility";
import { arrayDocument, Oplet } from "./array-document";
import { arrayDocument, deleteAt, insertAt, Oplet } from "./array-document";

const arrNum = arrayDocument<number>();

describeDocumentMeta('arrayDocument<number>', arrNum, {
singleton: [
[],
[{ typ: 'ins', idx: 0, val: 1 }],
[{ typ: 'del', idx: 0, val: 1 }],
[
{ typ: 'del', idx: 0, val: 1 },
{ typ: 'ins', idx: 1, val: 2 },
],
[
{ typ: 'ins', idx: 1, val: 2 },
{ typ: 'del', idx: 0, val: 1 },
],
insertAt(0, 1),
deleteAt(0, 1),
[...deleteAt(0, 1), ...insertAt(1, 2)],
[...insertAt(1, 2), ...deleteAt(0, 1)],
],
composable3: [
[
[],
[
{ typ: 'ins', idx: 1, val: 2 },
{ typ: 'del', idx: 0, val: 1 },
],
[
{ typ: 'del', idx: 0, val: 2 },
{ typ: 'ins', idx: 1, val: 2 },
],
[...insertAt(1, 2), ...deleteAt(0, 1)],
[...deleteAt(0, 2), ...insertAt(1, 2)],
],
[
[
{ typ: 'ins', idx: 1, val: 2 },
{ typ: 'del', idx: 0, val: 1 },
],
[...insertAt(1, 2), ...deleteAt(0, 1)],
[],
[
{ typ: 'del', idx: 0, val: 2 },
{ typ: 'ins', idx: 1, val: 2 },
],
[...deleteAt(0, 2), ...insertAt(1, 2)],
],
[
[
{ typ: 'ins', idx: 1, val: 2 },
{ typ: 'del', idx: 0, val: 1 },
],
[
{ typ: 'del', idx: 0, val: 2 },
{ typ: 'ins', idx: 1, val: 2 },
],
[...insertAt(1, 2), ...deleteAt(0, 1)],
[...deleteAt(0, 2), ...insertAt(1, 2)],
[],
],
[
insertAt(0, 1, 2, 3),
insertAt(1, 4, 5),
insertAt(3, 6),
],
[
insertAt(0, 0, 1, 2, 3),
deleteAt(2, 2, 3),
deleteAt(0, 0),
],
],
others({ comp, inv }) {
// const a: Oplet<number>[] = [{ typ: 'ins', idx: 1, val: 2}, { typ: 'del', idx: 0, val: 1 }];
Expand Down
13 changes: 10 additions & 3 deletions packages/@ot-doc/document/src/lib/array-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ export const arrayDocument = <T>({
const update = (...ops: Oplet<T>[]): void => {
if (r === a) r = [...a];
r.splice(i, 2, ...ops);
i = i + ops.length - 1;
i = Math.max(i + ops.length - 1, 0);
updated = true;
};

do {
updated = false;
i = 0;
while (i < r.length - 1) {
const [eA, eB] = r.slice(i, i + 2);
const eA = r[i];
const eB = r[i + 1];
if (eA.typ === 'ins') {
if (eB.typ === 'ins') {
if (eA.idx >= eB.idx) {
Expand Down Expand Up @@ -142,4 +143,10 @@ export const arrayDocument = <T>({
return true;
},
};
};
};

export const insertAt = <T>(i: number, ...arr: T[]): Oplet<T>[] =>
arr.map((val, j) => ({ typ: 'ins', val, idx: i + j }));

export const deleteAt = <T>(i: number, ...arr: T[]): Oplet<T>[] =>
arr.map((val, j): Oplet<T> => ({ typ: 'del', val, idx: i + j })).reverse();

0 comments on commit e5519a7

Please sign in to comment.