Cloudflare Pages / Build and verify the site (push) Successful in 3m29s
Use the verified dependency lock across native hosts and bundled apps. Preserve saved styles, local app reactivation, and offline packages while integrating the latest practice, Habits, Lumi, and figure preview updates.
332 lines
12 KiB
Ziran
332 lines
12 KiB
Ziran
// The Tai Chi course loads, and the app's motion and figure match the course
|
|
// studio's: sampled poses, solved joints and the flat figure's paint
|
|
// operations are compared with tests/fixtures/tai-chi-reference.json,
|
|
// written by design/tai-chi/tools/export-app.mjs from the same course.tch.
|
|
// A filled shape is compared by its area, centroid and bounds, a line by its
|
|
// ends and number of points.
|
|
#import "file"
|
|
#import "json_scan"
|
|
#import, file "../apps/practice/course_access.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_math.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_course.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_timeline.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_solve.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_view.zi";
|
|
#import, file "../src/practices/tai_chi/tai_chi_flat.zi";
|
|
|
|
test_timeline: TaiChiTimeline;
|
|
test_worst: float64;
|
|
test_frame: TaiChiFlatFrame;
|
|
|
|
TestNumber :: (text: string, at: s32) -> float64 {
|
|
return ReadNumber(text, at).value
|
|
}
|
|
|
|
TestElementNumber :: (text: string, array_at: s32, index: s32) -> float64 {
|
|
return TestNumber(text, ElementAt(text, array_at, index))
|
|
}
|
|
|
|
TestStringIs :: (text: string, at: s32, expected: string) -> bool {
|
|
span := ReadString(text, at)
|
|
return span.valid && SpanEqualsASCII(text, span, expected)
|
|
}
|
|
|
|
TestClose :: (got: float64, want: float64, tolerance: float64) -> bool {
|
|
difference := TaiChiAbs(got - want)
|
|
if difference > test_worst {
|
|
test_worst = difference
|
|
}
|
|
return difference <= tolerance
|
|
}
|
|
|
|
TestVectorClose :: (got: TaiChiVector3, text: string, at: s32, tolerance: float64) -> bool {
|
|
return TestClose(got.x, TestElementNumber(text, at, 0), tolerance) &&
|
|
TestClose(got.y, TestElementNumber(text, at, 1), tolerance) &&
|
|
TestClose(got.z, TestElementNumber(text, at, 2), tolerance)
|
|
}
|
|
|
|
TestJoint :: (solved: *TaiChiSolved, index: s32) -> TaiChiVector3 {
|
|
if index == 0 { return solved.pelvis }
|
|
if index == 1 { return solved.neck_base }
|
|
if index == 2 { return solved.shoulder_center }
|
|
if index == 3 { return solved.shoulders[0] }
|
|
if index == 4 { return solved.elbows[1] }
|
|
if index == 5 { return solved.wrists[0] }
|
|
if index == 6 { return solved.wrists[1] }
|
|
if index == 7 { return solved.knees[0] }
|
|
if index == 8 { return solved.knees[1] }
|
|
if index == 9 { return solved.ankles[0] }
|
|
if index == 10 { return solved.heels[1] }
|
|
if index == 11 { return solved.toes[0] }
|
|
if index == 12 { return solved.head_top }
|
|
return solved.com
|
|
}
|
|
|
|
// A "#rrggbb" colour as 0xRRGGBB.
|
|
TestColor :: (text: string, at: s32) -> u32 {
|
|
span := ReadString(text, at)
|
|
color: u32 = 0
|
|
if !span.valid || span.end - span.start != 7 {
|
|
return 0xffffffff
|
|
}
|
|
for index: span.start + 1..span.end - 1 {
|
|
digit := cast(u32)text[index]
|
|
value: u32 = 0
|
|
if digit >= cast(u32)48 && digit <= cast(u32)57 {
|
|
value = digit - cast(u32)48
|
|
} else if digit >= cast(u32)97 && digit <= cast(u32)102 {
|
|
value = digit - cast(u32)87
|
|
} else {
|
|
return 0xffffffff
|
|
}
|
|
color = (color << cast(u32)4) | value
|
|
}
|
|
return color
|
|
}
|
|
|
|
// Area, centroid and bounds of a filled shape against the reference.
|
|
TestFill :: (text: string, entry: s32, points: []TaiChiVector2) -> bool {
|
|
area: float64 = 0.0
|
|
cx: float64 = 0.0
|
|
cy: float64 = 0.0
|
|
low := points[0]
|
|
high := points[0]
|
|
count := cast(s32)points.count
|
|
for index: cast(s32)0..count - 1 {
|
|
a := points[index]
|
|
b := points[(index + 1) % count]
|
|
cross := a.x * b.y - b.x * a.y
|
|
area += cross
|
|
cx += (a.x + b.x) * cross
|
|
cy += (a.y + b.y) * cross
|
|
low = TaiChiV2(TaiChiMin(low.x, a.x), TaiChiMin(low.y, a.y))
|
|
high = TaiChiV2(TaiChiMax(high.x, a.x), TaiChiMax(high.y, a.y))
|
|
}
|
|
area = area / 2.0
|
|
if TaiChiAbs(area) > 0.000000000001 {
|
|
cx = cx / (6.0 * area)
|
|
cy = cy / (6.0 * area)
|
|
} else {
|
|
cx = points[0].x
|
|
cy = points[0].y
|
|
}
|
|
center := FieldAt(text, entry, "center")
|
|
box := FieldAt(text, entry, "box")
|
|
return TestClose(area, TestNumber(text, FieldAt(text, entry, "area")), 0.00002) &&
|
|
TestClose(cx, TestElementNumber(text, center, 0), 0.0005) &&
|
|
TestClose(cy, TestElementNumber(text, center, 1), 0.0005) &&
|
|
TestClose(low.x, TestElementNumber(text, box, 0), 0.0005) &&
|
|
TestClose(low.y, TestElementNumber(text, box, 1), 0.0005) &&
|
|
TestClose(high.x, TestElementNumber(text, box, 2), 0.0005) &&
|
|
TestClose(high.y, TestElementNumber(text, box, 3), 0.0005)
|
|
}
|
|
|
|
// The ends and number of points of a line against the reference.
|
|
TestLine :: (text: string, entry: s32, op: TaiChiFlatOp, points: []TaiChiVector2) -> bool {
|
|
ends := FieldAt(text, entry, "ends")
|
|
last := points[points.count - 1]
|
|
return cast(s32)TestNumber(text, FieldAt(text, entry, "count")) == op.count &&
|
|
TestClose(op.width, TestNumber(text, FieldAt(text, entry, "width")), 0.000002) &&
|
|
TestClose(points[0].x, TestElementNumber(text, ends, 0), 0.0005) &&
|
|
TestClose(points[0].y, TestElementNumber(text, ends, 1), 0.0005) &&
|
|
TestClose(last.x, TestElementNumber(text, ends, 2), 0.0005) &&
|
|
TestClose(last.y, TestElementNumber(text, ends, 3), 0.0005)
|
|
}
|
|
|
|
// One reference sample: 0 when it matches, otherwise a code for what
|
|
// differs.
|
|
TestSample :: (text: string, sample: s32) -> s32 {
|
|
character: s32 = -1
|
|
for index: cast(s32)0..tai_chi_course.character_count - 1 {
|
|
if TestStringIs(text, FieldAt(text, sample, "character"), TaiChiCourseString(tai_chi_course.characters[index].id)) {
|
|
character = index
|
|
}
|
|
}
|
|
if character < 0 {
|
|
return 13
|
|
}
|
|
item := cast(s32)TestNumber(text, FieldAt(text, sample, "item"))
|
|
if item < 0 || item >= tai_chi_course.item_count {
|
|
return 10
|
|
}
|
|
if !TaiChiTimelineForItem(*test_timeline, tai_chi_course.items[item]) {
|
|
return 11
|
|
}
|
|
time := TestNumber(text, FieldAt(text, sample, "time"))
|
|
pose := TaiChiTimelineSample(*test_timeline, time)
|
|
channels := FieldAt(text, sample, "channels")
|
|
for channel: cast(s32)0..TAI_CHI_CHANNEL_COUNT - 1 {
|
|
if !TestClose(pose.channels[channel], TestElementNumber(text, channels, channel), 0.0002) {
|
|
return 100 + channel
|
|
}
|
|
}
|
|
rig := TaiChiRigFor(character)
|
|
solved := TaiChiSolvePose(*pose, *rig)
|
|
joints := FieldAt(text, sample, "joints")
|
|
for joint: cast(s32)0..13 {
|
|
if !TestVectorClose(TestJoint(*solved, joint), text, ElementAt(text, joints, joint), 0.0005) {
|
|
return 200 + joint
|
|
}
|
|
}
|
|
camera_at := FieldAt(text, sample, "camera")
|
|
center := FieldAt(text, camera_at, "center")
|
|
camera: TaiChiCamera
|
|
camera.yaw = TestNumber(text, FieldAt(text, camera_at, "yaw"))
|
|
camera.tilt = TestNumber(text, FieldAt(text, camera_at, "tilt"))
|
|
camera.center = TaiChiV3(TestElementNumber(text, center, 0), 0.0, TestElementNumber(text, center, 1))
|
|
TaiChiFlatPaint(*test_frame, *solved, *rig, character, camera)
|
|
ops := FieldAt(text, sample, "ops")
|
|
if ArrayCount(text, ops) != test_frame.op_count {
|
|
return 12
|
|
}
|
|
for index: cast(s32)0..test_frame.op_count - 1 {
|
|
entry := ElementAt(text, ops, index)
|
|
op := test_frame.ops[index]
|
|
points := test_frame.points[op.first:op.first + op.count]
|
|
fill_at := FieldAt(text, entry, "fill")
|
|
is_fill := fill_at >= 0
|
|
if is_fill != (op.kind == TAI_CHI_FLAT_FILL) {
|
|
return 300 + index
|
|
}
|
|
color_at := ifx is_fill then fill_at else FieldAt(text, entry, "line")
|
|
expected_color := TestColor(text, color_at)
|
|
expected_alpha := TestNumber(text, FieldAt(text, entry, "alpha"))
|
|
if expected_color != op.color || !TestClose(op.alpha, expected_alpha, 0.000002) {
|
|
print("tai chi item % at %s, paint op %: color % expected %, alpha % expected %\n",
|
|
item, time, index, op.color, expected_color, op.alpha, expected_alpha)
|
|
return 400 + index
|
|
}
|
|
matches := ifx is_fill then TestFill(text, entry, points) else TestLine(text, entry, op, points)
|
|
if !matches {
|
|
return 500 + index
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// The reading time of words `cue`, from a key that shows them.
|
|
TestReadingTime :: (timeline: *TaiChiTimeline, cue: s32) -> float64 {
|
|
for index: cast(s32)0..timeline.count - 1 {
|
|
if timeline.keys[index].cue == cue {
|
|
return timeline.keys[index].read
|
|
}
|
|
}
|
|
return 0.0
|
|
}
|
|
|
|
// Whether every run of the same words on screen lasts its reading time.
|
|
TestWordsReadable :: (timeline: *TaiChiTimeline) -> bool {
|
|
step: float64 = 0.02
|
|
shown: s32 = -1
|
|
since: float64 = 0.0
|
|
time: float64 = 0.0
|
|
while time <= timeline.duration + step {
|
|
cue := ifx time < timeline.duration then TaiChiTimelineCue(timeline, time) else -2
|
|
if cue != shown {
|
|
if shown >= 0 && time - since < TestReadingTime(timeline, shown) - 3.0 * step {
|
|
return false
|
|
}
|
|
shown = cue
|
|
since = time
|
|
}
|
|
time += step
|
|
}
|
|
return true
|
|
}
|
|
|
|
CheckPreview :: () -> s32 {
|
|
bytes, found := ReadEntireFile("build/tai-chi-zi-test/course-preview.tch")
|
|
if !found || !TaiChiCourseParse(*tai_chi_course, bytes) {
|
|
return 43
|
|
}
|
|
if TaiChiItemFree("full/normal") || TaiChiItemFree("lesson-4/all") ||
|
|
TaiChiItemFree("lesson-10/all") || TaiChiItemFree("lesson-1/") ||
|
|
!TaiChiItemFree("lesson-3/all") || !TaiChiItemFree("foundations/breathing") {
|
|
return 44
|
|
}
|
|
free: s32 = 0
|
|
for index: cast(s32)0..tai_chi_course.item_count - 1 {
|
|
item := tai_chi_course.items[index]
|
|
playable := TaiChiTimelineForItem(*test_timeline, item)
|
|
if TaiChiItemFree(TaiChiCourseString(item.id)) {
|
|
free += 1
|
|
if !playable || !TestClose(test_timeline.duration, item.duration, 0.01) {
|
|
return 45
|
|
}
|
|
} else if playable || item.source != TAI_CHI_SOURCE_LOCKED || item.mark_count != 0 {
|
|
return 46
|
|
}
|
|
}
|
|
if free != 44 {
|
|
return 47
|
|
}
|
|
return 0
|
|
}
|
|
|
|
#program_export
|
|
Check :: () -> s32 {
|
|
bytes, ok := ReadEntireFile("build/tai-chi-zi-test/course.tch")
|
|
if !ok {
|
|
return 1
|
|
}
|
|
if !TaiChiCourseParse(*tai_chi_course, bytes) {
|
|
return 2
|
|
}
|
|
archive, archive_ok := ReadEntireFile("build/tai-chi-zi-test/course.zip")
|
|
if !archive_ok || !TaiChiCourseParseArchive(*tai_chi_course, archive) {
|
|
return 40
|
|
}
|
|
if TaiChiCourseParseArchive(*tai_chi_course, archive[:archive.count / 2]) ||
|
|
TaiChiCourseParseArchive(*tai_chi_course, bytes) {
|
|
return 41
|
|
}
|
|
if !TaiChiCourseParseArchive(*tai_chi_course, archive) {
|
|
return 42
|
|
}
|
|
if tai_chi_course.key_set_count < 2 || tai_chi_course.character_count != 4 || tai_chi_course.module_count != 3 {
|
|
return 3
|
|
}
|
|
if TaiChiCourseItemIndex("full/normal") < 0 || TaiChiCourseString(tai_chi_course.characters[0].id) != "jadeWoman" ||
|
|
TaiChiCourseString(tai_chi_course.characters[1].id) != "indigoMan" ||
|
|
TaiChiCourseString(tai_chi_course.characters[2].id) != "tibetanWoman" ||
|
|
TaiChiCourseString(tai_chi_course.characters[3].id) != "monk" {
|
|
return 4
|
|
}
|
|
// Every item builds a timeline that fits and lasts as long as in the
|
|
// studio, and in items taught step by step every step's words stay on
|
|
// screen long enough to read.
|
|
for index: cast(s32)0..tai_chi_course.item_count - 1 {
|
|
item := tai_chi_course.items[index]
|
|
if !TaiChiTimelineForItem(*test_timeline, item) {
|
|
return 5
|
|
}
|
|
if !TestClose(test_timeline.duration, item.duration, 0.01) {
|
|
return 8
|
|
}
|
|
if item.step_words && !TestWordsReadable(*test_timeline) {
|
|
return 9
|
|
}
|
|
}
|
|
reference, found := ReadEntireFile("tests/fixtures/tai-chi-reference.json")
|
|
if !found {
|
|
return 6
|
|
}
|
|
samples := FieldAt(reference, 0, "samples")
|
|
count := ArrayCount(reference, samples)
|
|
if count < 10 {
|
|
return 7
|
|
}
|
|
for index: cast(s32)0..count - 1 {
|
|
result := TestSample(reference, ElementAt(reference, samples, index))
|
|
if result != 0 {
|
|
return result * 100 + index
|
|
}
|
|
}
|
|
return CheckPreview()
|
|
}
|
|
|
|
#program_export
|
|
CheckWorst :: () -> float64 {
|
|
return test_worst
|
|
}
|