Viewing is open to everyone. Downloading and starring need a GitHub sign-in.
Sign in with GitHub
SymbolTests.cs
v0.1.0-build.85
Latest
Commit ede38f5
Sign in with GitHub to download. Each download is recorded with your GitHub username.
| 1 | using System; |
| 2 | using System.Text; |
| 3 | using Xunit; |
| 4 | |
| 5 | namespace OpenCKey.Tests |
| 6 | { |
| 7 | public class SymbolTests |
| 8 | { |
| 9 | [Fact] |
| 10 | public void EncodesText() |
| 11 | { |
| 12 | var symbol = CKeySymbol.Encode("https://openckey.org/hello"); |
| 13 | Assert.InRange(symbol.Version, 1, CKeySymbol.MaxVersion); |
| 14 | Assert.Equal(26, symbol.PayloadLength); |
| 15 | Assert.Equal(EccLevel.M, symbol.EccLevel); |
| 16 | Assert.Equal(Layout.Standard, symbol.Layout); |
| 17 | Assert.InRange(symbol.ModulesAcross, 29, 232); |
| 18 | Assert.Equal(symbol.CodewordLength, symbol.GetCodeword().Length); |
| 19 | } |
| 20 | |
| 21 | [Fact] |
| 22 | public void HonoursVersionLevelAndLayout() |
| 23 | { |
| 24 | var symbol = CKeySymbol.Encode(Encoding.ASCII.GetBytes("abc"), version: 5, level: EccLevel.H, layout: Layout.Print); |
| 25 | Assert.Equal(5, symbol.Version); |
| 26 | Assert.Equal(EccLevel.H, symbol.EccLevel); |
| 27 | Assert.Equal(Layout.Print, symbol.Layout); |
| 28 | } |
| 29 | |
| 30 | [Fact] |
| 31 | public void Capacities() |
| 32 | { |
| 33 | Assert.Equal(0, CKeySymbol.Capacity(0)); |
| 34 | Assert.Equal(0, CKeySymbol.Capacity(32)); |
| 35 | Assert.True(CKeySymbol.Capacity(31) > CKeySymbol.Capacity(1)); |
| 36 | Assert.Equal(4619, CKeySymbol.Capacity(31, EccLevel.M)); // spec/CKey-Payload-Conventions.md |
| 37 | Assert.Equal(29, CKeySymbol.ModulesAcrossFor(1)); |
| 38 | Assert.Equal(232, CKeySymbol.ModulesAcrossFor(31)); |
| 39 | } |
| 40 | |
| 41 | [Fact] |
| 42 | public void RejectsBadArguments() |
| 43 | { |
| 44 | Assert.Throws<ArgumentNullException>(() => CKeySymbol.Encode((string)null!)); |
| 45 | Assert.Throws<ArgumentOutOfRangeException>(() => CKeySymbol.Encode("x", version: 32)); |
| 46 | Assert.Throws<ArgumentOutOfRangeException>(() => CKeySymbol.Encode("x", version: -1)); |
| 47 | var tooBig = new byte[CKeySymbol.Capacity(1) + 1]; |
| 48 | var e = Assert.Throws<CKeyException>(() => CKeySymbol.Encode(tooBig, version: 1)); |
| 49 | Assert.NotEqual(CKeyStatus.Ok, e.Status); |
| 50 | } |
| 51 | |
| 52 | [Fact] |
| 53 | public void RendersOpaqueAndTransparentRgba() |
| 54 | { |
| 55 | var symbol = CKeySymbol.Encode("hello"); |
| 56 | CKeyImage opaque = symbol.RenderRgba(256); |
| 57 | Assert.Equal(256, opaque.Width); |
| 58 | Assert.Equal(256, opaque.Height); |
| 59 | Assert.Equal(256 * 256 * 4, opaque.Rgba.Length); |
| 60 | Assert.Equal(255, opaque.Rgba[3]); // corner pixel: painted background |
| 61 | |
| 62 | CKeyImage clear = symbol.RenderRgba(256, RasterPolicy.Binary, transparent: true); |
| 63 | Assert.Equal(0, clear.Rgba[3]); // quiet margin: unpainted |
| 64 | Assert.Throws<ArgumentOutOfRangeException>(() => symbol.RenderRgba(8)); |
| 65 | } |
| 66 | |
| 67 | [Fact] |
| 68 | public void RenderIsDeterministic() |
| 69 | { |
| 70 | var symbol = CKeySymbol.Encode("determinism"); |
| 71 | Assert.Equal(symbol.RenderRgba(200).Rgba, symbol.RenderRgba(200).Rgba); |
| 72 | } |
| 73 | |
| 74 | [Fact] |
| 75 | public void Exports() |
| 76 | { |
| 77 | var symbol = CKeySymbol.Encode("exports"); |
| 78 | byte[] png = symbol.ToPng(128); |
| 79 | Assert.Equal(new byte[] { 0x89, (byte)'P', (byte)'N', (byte)'G' }, png[..4]); |
| 80 | byte[] grayPng = symbol.RenderGray(128).ToPng(oneBit: true); |
| 81 | Assert.Equal(0x89, grayPng[0]); |
| 82 | string svg = symbol.ToSvg(); |
| 83 | Assert.Contains("<svg", svg); |
| 84 | Assert.EndsWith(">", svg.TrimEnd()); |
| 85 | byte[] pdf = symbol.ToPdf(); |
| 86 | Assert.Equal("%PDF", Encoding.ASCII.GetString(pdf, 0, 4)); |
| 87 | } |
| 88 | |
| 89 | [Fact] |
| 90 | public void PrintRasterIsBinary() |
| 91 | { |
| 92 | var symbol = CKeySymbol.Encode("print", layout: Layout.Print); |
| 93 | CKeyGrayImage print = symbol.RenderPrint(1.0, 300); |
| 94 | Assert.InRange(print.Width, 290, 310); |
| 95 | Assert.All(print.Gray, v => Assert.True(v == 0 || v == 255)); |
| 96 | } |
| 97 | |
| 98 | [Fact] |
| 99 | public void StylePresetsAndChecks() |
| 100 | { |
| 101 | CKeyStyle plain = CKeyStyle.Plain; |
| 102 | Assert.Equal(CKeyColor.Black, plain.Ink); |
| 103 | Assert.Equal(CKeyColor.White, plain.Background); |
| 104 | Assert.Equal(StylePolicy.Strict, plain.Policy); |
| 105 | CKeyStyle concept = CKeyStyle.Concept; |
| 106 | Assert.NotEqual(plain.Background, concept.Background); |
| 107 | |
| 108 | var symbol = CKeySymbol.Encode("styled"); |
| 109 | Assert.True(symbol.CheckStyle(concept, out _)); |
| 110 | Assert.Equal(512, symbol.RenderRgba(512, style: concept).Width); |
| 111 | |
| 112 | var unreadable = CKeyStyle.Plain; |
| 113 | unreadable.Ink = new CKeyColor(250, 250, 250); // white on white |
| 114 | Assert.False(symbol.CheckStyle(unreadable, out string why)); |
| 115 | Assert.False(string.IsNullOrEmpty(why)); |
| 116 | Assert.Throws<CKeyException>(() => symbol.RenderRgba(256, style: unreadable)); |
| 117 | } |
| 118 | |
| 119 | [Fact] |
| 120 | public void Certification() |
| 121 | { |
| 122 | var symbol = CKeySymbol.Encode("certify"); |
| 123 | CKeyCertification concept = symbol.Certify(CKeyStyle.Concept); |
| 124 | Assert.True(concept.IsAccepted, concept.Message); |
| 125 | Assert.True(concept.IsCertified, concept.Message); |
| 126 | // The photo sweep goes down to sizes too small to read, so a |
| 127 | // certified style passes some trials, not necessarily all. |
| 128 | Assert.InRange(concept.TrialsPassed, 1, concept.Trials); |
| 129 | |
| 130 | // Transparent output over an unknown background is never certified. |
| 131 | CKeyCertification unknown = symbol.Certify(null, transparent: true); |
| 132 | Assert.False(unknown.IsCertified); |
| 133 | Assert.Contains("background unknown", unknown.Message); |
| 134 | |
| 135 | CKeyCertification onWhite = symbol.Certify(null, transparent: true, backdropColours: new[] { CKeyColor.White }); |
| 136 | Assert.True(onWhite.IsCertified, onWhite.Message); |
| 137 | } |
| 138 | |
| 139 | [Fact] |
| 140 | public void ColourParsing() |
| 141 | { |
| 142 | Assert.Equal(new CKeyColor(0x12, 0x34, 0x56), CKeyColor.Parse("#123456")); |
| 143 | Assert.Equal("#abcdef", CKeyColor.FromRgb(0xabcdef).ToString()); |
| 144 | Assert.Throws<FormatException>(() => CKeyColor.Parse("#12345")); |
| 145 | } |
| 146 | } |
| 147 | } |