Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions tests/TdbIO.Tests/VectorTests.Drawings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Buffers.Binary;
using System.Text;
using System.Text.Json;
using TdbIO.Internal;
using Xunit;

namespace TdbIO.Tests;

/// <summary>The drawing table and the drawing-file container against the private vectors. Entries are selected by the
/// keys of their expected values, never by name.</summary>
public partial class VectorTests
{
[SkippableFact]
public void DrawingTableVectorDecodesAsExpected()
{
foreach (var entry in TableJson.Selected(TestVectors.WithKeys("drawings"), "drawing tables"))
{
string name = TestVectors.Name(entry);
byte[] data = TestVectors.Read(entry);
var expected = TableJson.Expected(entry);
var sections = SectionWalker.Walk(data, 0, expected.GetProperty("ordinal").GetInt32(), SectionMode.Db1, null);
var section = Assert.Single(sections);
Assert.Equal(expected.GetProperty("K").GetInt32(), section.Key);
Assert.Equal(expected.GetProperty("row_size").GetInt32(), section.RowSize);
Assert.Equal(expected.GetProperty("rows").GetInt32(), section.Rows.Count);
var drawings = section.AsDrawings();
var rows = expected.GetProperty("drawings").EnumerateArray().ToList();
Assert.Equal(rows.Count, drawings.Count);
for (int i = 0; i < rows.Count; i++)
{
var e = rows[i];
string at = $"{name}: drawing {i}";
Assert.True(e.GetProperty("id").GetInt32() == drawings[i].Id, $"{at}: id");
Assert.True(e.GetProperty("obj_type").GetInt32() == drawings[i].ObjType, $"{at}: obj_type");
Assert.Equal(e.GetProperty("mark").GetString(), drawings[i].Mark);
Assert.True(e.GetProperty("created").GetDouble() == drawings[i].CreatedAt, $"{at}: created");
Assert.True(e.GetProperty("modified").GetDouble() == drawings[i].ModifiedAt, $"{at}: modified");
Assert.Equal(e.GetProperty("file_name").GetString(), drawings[i].FileName);
Assert.Equal(e.GetProperty("type_name").GetString(), drawings[i].TypeName);
Assert.Equal((e.GetProperty("a").GetInt32(), e.GetProperty("b").GetInt32()),
(section.Rows[i].A, section.Rows[i].B));
}
Assert.Equal(expected.GetProperty("descriptors").EnumerateArray().Select(x => (uint)x.GetInt32()).ToArray(),
section.Descriptors.ToArray());
TestVectors.CheckKeys(entry, ["ordinal", "K", "row_size", "descriptors", "rows", "drawings"], []);
}
}

[SkippableFact]
public void DgContainerVectorMatchesTheHeader()
{
foreach (var entry in TableJson.Selected(TestVectors.WithKeys("list_count", "list"), ".dg containers"))
{
string name = TestVectors.Name(entry);
byte[] data = TestVectors.Read(entry);
var expected = TableJson.Expected(entry);
Assert.True(data[6] == expected.GetProperty("type_byte").GetByte(), $"{name}: type byte");
// The library recognises a drawing and does not read it. It does not read the id list either, so the checks of
// the list below compare the vector with its manifest only.
Assert.Equal(XsteelHeader.DrawingType, data[6]);
Assert.Equal(TdbFileKind.Drawing, TdbFile.DetectKind(new MemoryStream(Synthetic.Gzip(data))));
Assert.Throws<NotSupportedException>(() => Db1Document.Create(data));
Assert.Equal(expected.GetProperty("version").GetString(), Encoding.ASCII.GetString(data[8..12]));
int count = (int)BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(12));
Assert.Equal(expected.GetProperty("list_count").GetInt32(), count);

var ids = new List<uint>();
var flags = new List<byte>();
for (int q = 16; q + 5 <= 16 + 5 * (count + 1); q += 5)
{
ids.Add(BinaryPrimitives.ReadUInt32LittleEndian(data.AsSpan(q)));
flags.Add(data[q + 4]);
}
Assert.True(flags[^1] == 0 && flags.Take(flags.Count - 1).All(f => f == 4), $"{name}: flags");
Assert.Equal(expected.GetProperty("list").EnumerateArray().Select(x => x.GetUInt32()), ids);
int contentStart = 16 + 5 * (count + 1);
Assert.Equal(expected.GetProperty("content_start").GetInt32(), contentStart);
// The vector pins the first 16 bytes of the content area; the rest of the drawing is not part of it.
Assert.Equal(contentStart + 16, data.Length);
if (expected.TryGetProperty("first_content_hex", out var content))
Assert.Equal(content.GetString(), Convert.ToHexString(data[contentStart..]).ToLowerInvariant());
// The value of "flags" is not read: the flag bytes are checked against the pattern above.
TestVectors.CheckKeys(entry,
["type_byte", "version", "list_count", "list", "content_start", "first_content_hex"],
["flags", "content_word_at_11"]);
}
}
}
Loading