Skip to content

Commit 0bdc517

Browse files
committed
More tests for version and format information
1 parent 0af571e commit 0bdc517

2 files changed

Lines changed: 106 additions & 2 deletions

File tree

QrCodeGenerator/QrCodeBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ private static void ProtectFormatBits(BitMatrix protectedModules)
450450
protectedModules.FillRect(8, protectedModules.Size - 7, 1, 7);
451451
}
452452

453-
private static void DrawFormatInformation(BitMatrix modules, int ecc, int pattern)
453+
internal static void DrawFormatInformation(BitMatrix modules, int ecc, int pattern)
454454
{
455455
DrawFormatBits(modules, GetFormatInformationBits(ecc, pattern));
456456
}
457457

458-
private static void DrawFormatInformation(BitMatrix modules, BitMatrix transposed, int ecc, int pattern)
458+
internal static void DrawFormatInformation(BitMatrix modules, BitMatrix transposed, int ecc, int pattern)
459459
{
460460
DrawFormatBits(modules, transposed, GetFormatInformationBits(ecc, pattern));
461461
}

QrCodeGeneratorTest/QrCodeBuilderTest.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Diagnostics.CodeAnalysis;
1011
using System.Text;
1112
using Xunit;
1213

@@ -287,6 +288,7 @@ private static void AssertAlignmentPattern(BitMatrix modules, int cx, int cy)
287288
Assert.True(modules.Get(cx, cy), $"align center ({cx},{cy})");
288289
}
289290

291+
[SuppressMessage("csharpsquid", "S2234")]
290292
private static void AssertMirrored(BitMatrix matrix)
291293
{
292294
var size = matrix.Size;
@@ -336,6 +338,33 @@ private static int CountLightDataModules(BitMatrix modules, BitMatrix dataMask)
336338
return sum;
337339
}
338340

341+
[Theory, CombinatorialData]
342+
public void DrawVersionInformation([CombinatorialRange(7, 40, 1)] int version)
343+
{
344+
var modules = QrCodeBuilder.CreateWithFixedPatterns(version);
345+
var size = modules.Size;
346+
var expected = QrCodeBuilder.GetVersionInformationBits(version);
347+
348+
var bottomLeft = 0;
349+
var topRight = 0;
350+
for (var bit = 0; bit < 18; bit += 1)
351+
{
352+
var x = bit / 3;
353+
var y = bit % 3;
354+
if (modules.Get(x, size - 11 + y))
355+
{
356+
bottomLeft |= 1 << bit;
357+
}
358+
if (modules.Get(size - 11 + y, x))
359+
{
360+
topRight |= 1 << bit;
361+
}
362+
}
363+
364+
Assert.Equal(expected, bottomLeft);
365+
Assert.Equal(expected, topRight);
366+
}
367+
339368
[Theory, CombinatorialData]
340369
public void GetVersionInformationBits([CombinatorialRange(7, 40, 1)] int version)
341370
{
@@ -359,6 +388,81 @@ public void GetVersionInformationBits([CombinatorialRange(7, 40, 1)] int version
359388
Assert.Equal(0, remainder);
360389
}
361390

391+
[Theory, CombinatorialData]
392+
public void DrawFormatInformation(
393+
[CombinatorialRange(1, 40)] int version,
394+
[CombinatorialRange(0, 3, 1)] int ecc,
395+
[CombinatorialRange(0, 7, 1)] int pattern)
396+
{
397+
var modules = QrCodeBuilder.CreateWithFixedPatterns(version);
398+
QrCodeBuilder.DrawFormatInformation(modules, ecc, pattern);
399+
400+
var expected = QrCodeBuilder.GetFormatInformationBits(ecc, pattern);
401+
Assert.Equal(expected, ExtractFormatInformationA(modules));
402+
Assert.Equal(expected, ExtractFormatInformationB(modules));
403+
}
404+
405+
[Theory, CombinatorialData]
406+
public void DrawFormatInformationTransposed(
407+
[CombinatorialRange(1, 40)] int version,
408+
[CombinatorialRange(0, 3, 1)] int ecc,
409+
[CombinatorialRange(0, 7, 1)] int pattern)
410+
{
411+
var modules = QrCodeBuilder.CreateWithFixedPatterns(version);
412+
var transposed = modules.Copy();
413+
transposed.Transpose();
414+
415+
QrCodeBuilder.DrawFormatInformation(modules, transposed, ecc, pattern);
416+
417+
transposed.Transpose();
418+
419+
var expected = QrCodeBuilder.GetFormatInformationBits(ecc, pattern);
420+
Assert.Equal(expected, ExtractFormatInformationA(modules));
421+
Assert.Equal(expected, ExtractFormatInformationB(modules));
422+
Assert.Equal(expected, ExtractFormatInformationA(transposed));
423+
Assert.Equal(expected, ExtractFormatInformationB(transposed));
424+
}
425+
426+
private static int ExtractFormatInformationA(BitMatrix modules)
427+
{
428+
// Placement A (around top-left finder): column 8 going down (skipping row 6),
429+
// corner, then row 8 going left (skipping column 6).
430+
var placementA = 0;
431+
for (var i = 0; i < 6; i += 1)
432+
{
433+
if (modules.Get(8, i)) placementA |= 1 << i;
434+
}
435+
436+
if (modules.Get(8, 7)) placementA |= 1 << 6;
437+
if (modules.Get(8, 8)) placementA |= 1 << 7;
438+
if (modules.Get(7, 8)) placementA |= 1 << 8;
439+
for (var i = 9; i < 15; i += 1)
440+
{
441+
if (modules.Get(14 - i, 8)) placementA |= 1 << i;
442+
}
443+
444+
return placementA;
445+
}
446+
447+
private static int ExtractFormatInformationB(BitMatrix modules)
448+
{
449+
int size = modules.Size;
450+
451+
// Placement B: row 8 from right border going left (bits 0..7),
452+
// then column 8 from row size-7 going down to bottom border (bits 8..14).
453+
var placementB = 0;
454+
for (var i = 0; i < 8; i += 1)
455+
{
456+
if (modules.Get(size - 1 - i, 8)) placementB |= 1 << i;
457+
}
458+
for (var i = 8; i < 15; i += 1)
459+
{
460+
if (modules.Get(8, size - 15 + i)) placementB |= 1 << i;
461+
}
462+
463+
return placementB;
464+
}
465+
362466
[Theory, CombinatorialData]
363467
public void GetFormatInformationBits(
364468
[CombinatorialRange(0, 3, 1)] int ecc,

0 commit comments

Comments
 (0)