Files
models/tests/reading.sakura
T

186 lines
8.9 KiB
Plaintext
Raw Permalink Normal View History

2026-10-30 12:04:54 -05:00
#! Contains the tests of reading assets: from `.gltf` JSON with its buffer in a
#! `data:` URI, from a `.glb` file, and from files beside it; and what's refused,
#! each with why.
import std::io;
import std::testing;
import malunal::linear;
import malunal::gltf;
# Checks what the quad holds, read any way.
def expect_quad(t: &mut tester, read: &gltf::asset): void {
t.expects(read.version.span == "2.0" && read.generator.span == "gltf tests");
t.expects(read.scene is some(s) && s == 0 && read.scenes.length == 1 && read.scenes[0].name.span == "Quad");
t.expects(read.nodes.length == 1 && read.nodes[0].name.span == "Square" && read.nodes[0].mesh is some(m) && m == 0);
t.expects(near(read.nodes[0].translation.y, 2) && near(read.nodes[0].matrix.m[14], 3));
val part := &read.meshes[0].primitives[0];
t.expects(part.mode == gltf::topology::triangles && part.find("POSITION") is some(p) && p == 0 && part.find("TEXCOORD_0") is none);
val places := read.floats(0);
t.expects(places.length == 12 && near(places[3], 0.5) && near(places[7], 0.5));
val normals := read.floats(1);
t.expects(normals.length == 12 && near(normals[2], 1));
val indices := read.integers(2);
t.expects(indices.length == 6 && indices[4] == 2 && indices[5] == 3);
val red := &read.materials[0];
t.expects(red.name.span == "red" && red.double_sided && near(red.base_color[0], 1) && near(red.base_color[1], 0));
t.expects(near(red.metallic, 0) && near(red.roughness, 1) && red.alpha_mode == gltf::alpha_mode::opaque && red.base_color_texture is none);
t.expects(read.accessors[0].max.length == 3 && read.accessors[0].max[0] == 0.5);
}
@test def reads_json_with_its_buffer_in_a_data_uri(t: &mut tester): void {
val bytes := quad_buffer();
val uri := data_uri(bytes.span);
val json := quad_json(uri.span);
val read := gltf::decode(json.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_quad(t, &read);
}
@test def reads_glb_files(t: &mut tester): void {
val json := quad_json("");
val bytes := quad_buffer();
val glb := glb_of(json.span, bytes.span);
put("quad.glb", glb.span);
val read := gltf::load(".blossom/gltf tests/quad.glb") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_quad(t, &read);
t.expects(read.buffers[0].uri.length == 0 && read.buffers[0].bytes.length >= 108);
}
# A buffer and an image as files beside the asset, the buffer's named with a
# space, percent-encoded, and an image in a buffer view of a `.glb` file.
@test def reads_buffers_and_images_from_files_beside_it(t: &mut tester): void {
val bytes := quad_buffer();
put("beside/quad data.bin", bytes.span);
val picture: u8[8] = [0x89, 'P', 'N', 'G', 13, 10, 26, 10];
put("beside/red.png", picture[:]);
val json := quad_json("quad%20data.bin", ", \"images\": [{ \"uri\": \"red.png\" }], \"textures\": [{ \"source\": 0 }]");
put("beside/quad.gltf", json.span);
val read := gltf::load(".blossom/gltf tests/beside/quad.gltf") else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
expect_quad(t, &read);
t.expects(read.images.length == 1 && read.images[0].bytes.length == 8 && read.images[0].bytes[1] == 'P');
t.expects(read.textures[0].source is some(s) && s == 0 && read.textures[0].sampler is none);
# The same image, in a view of a `.glb` file's buffer, after the quad's.
var packed_bytes := quad_buffer();
packed_bytes.extend(picture[:]);
val inner := quad_json("", ", \"images\": [{ \"bufferView\": 2, \"mimeType\": \"image/png\" }]");
# Its buffer is the quad's and the image's: 116 bytes, the image a third view.
val with_image := replaced(replaced(inner.span, "\"byteLength\": 108", "\"byteLength\": 116").span,
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }",
"{ \"buffer\": 0, \"byteOffset\": 96, \"byteLength\": 12 }, { \"buffer\": 0, \"byteOffset\": 108, \"byteLength\": 8 }");
val glb := glb_of(with_image.span, packed_bytes.span);
val bound := gltf::decode(glb.span) else (e) {
t.expects(false);
io::println(e.describe().span);
return;
};
t.expects(bound.images[0].mime_type.span == "image/png" && bound.images[0].bytes.length == 8 && bound.images[0].bytes[3] == 'G');
}
# Two texts, one after the other.
def joined(a: strview, b: strview): string {
var made := string::from(a, nothing{});
made.append(b);
return made;
}
# Text with the first of some text replaced.
def replaced(text: strview, what: strview, with: strview): string {
var made := string::new(nothing{});
var i: u64 = 0;
var done := false;
while i < text.length {
if !done && i + what.length <= text.length && text.substr(i, what.length) == what {
made.append(with);
i += what.length;
done = true;
continue;
}
made.push(text[i]);
i++;
}
return made;
}
# Whether what's refused is refused as `kind`, saying `says` somewhere in why.
def refuses(t: &mut tester, json: strview, kind: gltf::failure, says: strview): void {
match gltf::decode(json) {
value(_) => {
io::println($"read, but shouldn't be: {json}".span);
t.expects(false);
}
error(e) => {
val why := e.describe();
val found := holds(why.span, says);
if e.kind != kind || !found {
io::println($"refused as `{why.span}`, not as `{says}`".span);
}
t.expects(e.kind == kind && found);
}
}
}
def holds(text: strview, part: strview): bool {
return true if part.length == 0;
var i: u64 = 0;
while i + part.length <= text.length {
return true if text.substr(i, part.length) == part;
i++;
}
return false;
}
@test def refuses_what_breaks_gltf(t: &mut tester): void {
refuses(t, "{ not json", gltf::failure::malformed, "JSON");
refuses(t, "{ \"asset\": { \"version\": \"1.0\" } }", gltf::failure::unsupported, "1.0");
refuses(t, "{ \"scenes\": [] }", gltf::failure::malformed, "version");
refuses(t, asset_with("\"extensionsRequired\": [\"KHR_draco_mesh_compression\"]").span, gltf::failure::unsupported, "KHR_draco_mesh_compression");
refuses(t, asset_with("\"nodes\": [{ \"mesh\": 0 }]").span, gltf::failure::malformed, "nodes[0]: its mesh is 0, of the 0 meshes");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, { \"children\": [0] }]").span, gltf::failure::malformed, "comes back to it");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}, { \"children\": [1] }]").span, gltf::failure::malformed, "child of both");
refuses(t, asset_with("\"nodes\": [{ \"children\": [1] }, {}], \"scenes\": [{ \"nodes\": [1] }]").span, gltf::failure::malformed, "not a root");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8 }]").span, gltf::failure::malformed, "buffers[0]");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 8, \"uri\": \"missing.bin\" }]").span, gltf::failure::unreadable, "missing.bin");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"https://example.com/a.bin\" }]").span, gltf::failure::unsupported, "another place");
refuses(t, asset_with("\"buffers\": [{ \"byteLength\": 4, \"uri\": \"data:application/octet-stream;base64,AAAA\" }]").span, gltf::failure::malformed, "3");
# An accessor reaching past its view, and one of a type glTF hasn't.
val one_view := "\"buffers\": [{ \"byteLength\": 12, \"uri\": \"data:application/octet-stream;base64,AAAAAAAAAAAAAAAA\" }], \"bufferViews\": [{ \"buffer\": 0, \"byteLength\": 12 }]";
refuses(t, asset_with(joined(one_view, ", \"accessors\": [{ \"bufferView\": 0, \"componentType\": 5126, \"count\": 2, \"type\": \"VEC2\" }]").span).span,
gltf::failure::malformed, "reach past");
refuses(t, asset_with(joined(one_view, ", \"accessors\": [{ \"bufferView\": 0, \"componentType\": 5124, \"count\": 1, \"type\": \"SCALAR\" }]").span).span,
gltf::failure::malformed, "componentType 5124");
val twelve := "\"buffers\": [{ \"byteLength\": 12, \"uri\": \"data:application/octet-stream;base64,AAAAAAAAAAAAAAAA\" }]";
refuses(t, asset_with(joined(twelve, ", \"bufferViews\": [{ \"buffer\": 0, \"byteOffset\": 8, \"byteLength\": 8 }]").span).span,
gltf::failure::malformed, "reaches byte 16");
# A `.glb` file of another version, and one cut short.
val none := vector::<u8>::new();
var glb := glb_of(asset_with("").span, none.span);
glb[4] = 1;
refuses(t, strview::over(glb.data, glb.length), gltf::failure::unsupported, "version 1");
glb[4] = 2;
refuses(t, strview::over(glb.data, 16), gltf::failure::malformed, "cut short");
}
# What an asset uses but doesn't require, of extensions it doesn't read, is
# read without them, and named among its extensions.
@test def reads_past_extensions_it_only_uses(t: &mut tester): void {
val json := asset_with("\"extensionsUsed\": [\"KHR_texture_transform\"], \"nodes\": [{ \"extensions\": { \"KHR_texture_transform\": {} } }]");
val read := gltf::decode(json.span) else (_) {
t.expects(false);
return;
};
t.expects(read.extensions.length == 1 && read.extensions[0].span == "KHR_texture_transform" && read.nodes.length == 1);
}