Files
cocos-engine/tests/animation/compression.test.ts
Leslie Leigh (李的序) 6d10f68ef8 AnimationCurve (#8409)
* AnimationCurve

* Mark curve frame values as uniquelyReferenced

* Adjust modules

* Update

* Manually implements KeyframeCurve.iterator

* TargetPath

* Update

* Fix BUG

* Fix

* Add size track

* Update

* Fix size; unit tests

* fix split animation (#24)

* CCONB animation clip; Remove integer track/channel/curve

* Optimize key shared curves

* Remove keyframecurve.empty

* Exotic animation

* Fix unit tests

* start/end -> left/right

* Curve range rename

* Easing methods

* Fix untyped track

* Fix

* fix upgradUnTypedTracks (#26)

* Rename

.interpMode -> .interpolationMode
RealInterpMode -> RealInterpolationMode
QuaternionInterpMode -> QuaternionInterpolationMode
ExtrapMode -> ExtrapolationMode
.preExtrap -> .preExtrapolation
.postExtrap -> .postExtrapolation
QuaternionCurve -> QuatCurve
QuaternionKeyframeValue -> QuatKeyframeValue

* Update pre/post extrap

* Optimize-1

* Optimize-2

* Eliminate for-of

* Mark deprecateds

* Hide RealKeyframeValue/QuatKeyframeValue Constructor

* Fix quat keyframe value name

* Error ID

* Fix RealKeyframeValue editor extras

* Elimanate for-of forEach

Co-authored-by: zheng han <sunstar198941@gmail.com>
2021-07-23 11:26:55 +08:00

90 lines
3.1 KiB
TypeScript

import { removeLinearKeys, removeTrivialKeys } from '../../cocos/core/animation/compression';
describe('Curve compression', () => {
describe('Remove linear keys', () => {
test('Curves with only zero/one/two keys are not effected', () => {
expect(runRemoveLinearKeys([], [])).toBeRemovedWith([]);
expect(runRemoveLinearKeys([0.3], [0.4])).toBeRemovedWith([]);
expect(runRemoveLinearKeys([0.3, 0.4], [4.0, 5.0])).toBeRemovedWith([]);
});
test('Linear', () => {
expect(runRemoveLinearKeys([0.3, 0.4, 0.5], [3.0, 4.0, 5.0])).toBeRemovedWith([1]);
expect(runRemoveLinearKeys([0.3, 0.4, 0.5, 0.7], [3.0, 4.0, 5.0, 10.0])).toBeRemovedWith([1]);
});
test('Successive linear', () => {
expect(runRemoveLinearKeys([0.3, 0.4, 0.5, 0.6], [3.0, 4.0, 5.0, 6.0])).toBeRemovedWith([1, 2]);
expect(runRemoveLinearKeys([0.3, 0.4, 0.5, 0.6, 0.7], [3.0, 4.0, 5.0, 6.0, 10.0])).toBeRemovedWith([1, 2]);
});
test('Max error', () => {
expect(runRemoveLinearKeys([0.3, 0.4, 0.5], [3.0, 4.0002, 5.0], 1e-3)).toBeRemovedWith([1]);
expect(runRemoveLinearKeys([0.3, 0.4, 0.5], [3.0, 4.002, 5.0], 1e-3)).toBeRemovedWith([]);
});
});
describe('Remove trivial keys', () => {
test('Zero/One length', () => {
expect(runRemoveTrivialKeys([], [])).toBeRemovedWith([]);
expect(runRemoveTrivialKeys([0.3], [0.4])).toBeRemovedWith([]);
});
});
});
type CompressionResult = ReturnType<typeof runCompression>;
function runCompression<TArgs extends any[]>(
keys: number[],
values: number[],
fn: (keys: number[], values: number[], ...args: TArgs) => { keys: number[], values: number[] },
...args: TArgs
) {
const { keys: newKeys, values: newValues } = fn(keys, values, ...args);
return {
keys, values,
newKeys, newValues,
};
}
function runRemoveLinearKeys (keys: number[], values: number[], maxError?: number) {
return runCompression(
keys,
values,
removeLinearKeys,
maxError,
);
}
function runRemoveTrivialKeys (keys: number[], values: number[], maxError?: number) {
return runCompression(
keys,
values,
removeTrivialKeys,
maxError,
);
}
expect.extend({
toBeRemovedWith(received: CompressionResult, removals: number[]): jest.CustomMatcherResult {
const { keys, values, newKeys, newValues } = received;
const actualRemovals = keys.map((_, index) => index).filter((index) => !newKeys.includes(keys[index]));
return {
pass: actualRemovals.length === removals.length &&
actualRemovals.every((actualRemoval, index) => actualRemoval === removals[index]),
message: () =>
`Expected removals: ${removals.join(', ')}\n` +
`Actual removals: ${actualRemovals.join(', ')}`,
};
},
});
declare global {
namespace jest {
interface Matchers<R> {
toBeRemovedWith(expected: number[]): CustomMatcherResult
}
}
}