UnitTest1.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Xunit;
  2. using System.Text;
  3. using ZXingCpp;
  4. namespace ZXingCpp.Tests;
  5. public class UnitTest1
  6. {
  7. [Fact]
  8. public void ValidBarcodeFormatsParsing()
  9. {
  10. Assert.Equal(BarcodeFormats.QRCode, Barcode.FormatsFromString("qrcode"));
  11. Assert.Equal(BarcodeFormats.LinearCodes, Barcode.FormatsFromString("linear_codes"));
  12. Assert.Equal(BarcodeFormats.None, Barcode.FormatsFromString(""));
  13. }
  14. [Fact]
  15. public void InvalidBarcodeFormatsParsing()
  16. {
  17. Assert.Throws<Exception>(() => Barcode.FormatsFromString("nope"));
  18. }
  19. [Fact]
  20. public void InvalidImageView()
  21. {
  22. Assert.Throws<Exception>(() => new ImageView(new byte[0], 1, 1, ImageFormat.Lum));
  23. Assert.Throws<Exception>(() => new ImageView(new byte[1], 1, 1, ImageFormat.Lum, 2));
  24. }
  25. [Fact]
  26. public void Read()
  27. {
  28. var data = new List<byte>();
  29. foreach (var v in "0000101000101101011110111101011011101010100111011100101000100101110010100000")
  30. data.Add((byte)(v == '0' ? 255 : 0));
  31. var iv = new ImageView(data.ToArray(), data.Count, 1, ImageFormat.Lum);
  32. var br = new BarcodeReader() {
  33. Binarizer = Binarizer.BoolCast,
  34. };
  35. var res = br.From(iv);
  36. var expected = "96385074";
  37. Assert.Single(res);
  38. Assert.True(res[0].IsValid);
  39. Assert.Equal(BarcodeFormats.EAN8, res[0].Format);
  40. Assert.Equal(expected, res[0].Text);
  41. Assert.Equal(Encoding.ASCII.GetBytes(expected), res[0].Bytes);
  42. Assert.False(res[0].HasECI);
  43. Assert.Equal(ContentType.Text, res[0].ContentType);
  44. Assert.Equal(0, res[0].Orientation);
  45. Assert.Equal(new PointI() { X = 4, Y = 0 }, res[0].Position.TopLeft);
  46. Assert.Equal(1, res[0].LineCount);
  47. Assert.False(res[0].IsMirrored);
  48. Assert.False(res[0].IsInverted);
  49. Assert.Equal(ErrorType.None, res[0].ErrorType);
  50. Assert.Equal("", res[0].ErrorMsg);
  51. }
  52. [Fact]
  53. public void Create()
  54. {
  55. var text = "hello";
  56. var res = new Barcode(text, BarcodeFormats.DataMatrix);
  57. Assert.True(res.IsValid);
  58. Assert.Equal(BarcodeFormats.DataMatrix, res.Format);
  59. Assert.Equal(text, res.Text);
  60. Assert.Equal(Encoding.ASCII.GetBytes(text), res.Bytes);
  61. Assert.False(res.HasECI);
  62. Assert.Equal(ContentType.Text, res.ContentType);
  63. Assert.Equal(0, res.Orientation);
  64. Assert.False(res.IsMirrored);
  65. Assert.False(res.IsInverted);
  66. Assert.Equal(new PointI() { X = 1, Y = 1 }, res.Position.TopLeft);
  67. Assert.Equal(ErrorType.None, res.ErrorType);
  68. Assert.Equal("", res.ErrorMsg);
  69. }
  70. }