﻿using NUnit.Framework;

namespace Codebook.Runtime{
  [TestFixture]
  public class ByteStringReaderWriterTest {
    [Test]
    public void OverByteWriting() {
      var bytes = ByteStringBitsWriter.write(
        writer => {
          for (int i = 1; i < 6; i++) {
            writer.writeBits(i, i);
          }
        }
      );

      Assert.AreEqual(bytes.Length, 2);
      Assert.AreEqual((int)bytes[0] & 0xff, 0xcd);
      Assert.AreEqual((int)bytes[1] & 0xff, 0x0a);

      var reader = new ByteStringBitsReader(new ByteStringReader(bytes));
      Assert.AreEqual((int)reader.ReadBits(1), 1);
      Assert.AreEqual((int)reader.ReadBits(2), 2);
      Assert.AreEqual((int)reader.ReadBits(3), 3);
      Assert.AreEqual((int)reader.ReadBits(4), 4);
      Assert.AreEqual((int)reader.ReadBits(5), 5);
    }

    [Test]
    public void WritingThenReading() {
      var bytes = ByteStringBitsWriter.write(
          writer => {
            for (int i = 1;i < 32;i++) {
              writer.writeBits(i, i);
            }
          }
        );
      var reader = new ByteStringBitsReader(new ByteStringReader(bytes));
      for (int i = 1; i < 32; i++) {
        Assert.AreEqual((int)reader.ReadBits(i),i);
      }
    }
  }
}