<commit_before>﻿using kh.kh2;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace kh.tests.kh2
{
    public class BarTests
    {
        private const string FilePath = "kh2/res/bar_test.bar";

        [Fact]
        public void IsValidTest()
        {
            using (var stream = new MemoryStream())
            {
                stream.WriteByte(0x42);
                stream.WriteByte(0x41);
                stream.WriteByte(0x52);
                stream.WriteByte(0x01);
                stream.Position = 0;
                Assert.True(Bar.IsValid(stream));
            }
        }

        [Fact]
        public void IsNotValidTest()
        {
            using (var stream = new MemoryStream())
            {
                stream.WriteByte(1);
                stream.WriteByte(2);
                stream.WriteByte(3);
                stream.WriteByte(4);
                stream.Position = 0;
                Assert.False(Bar.IsValid(stream));
            }
        }

        [Fact]
        public void IsReadingEntryCountCorrect() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream);
                Assert.Equal(7, entries.Count());
            });

        [Fact]
        public void IsReadingEntryNamesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal("s_da", entries[0].Name);
                Assert.Equal("m_da", entries[1].Name);
                Assert.Equal("l_da", entries[2].Name);
                Assert.Equal("thru", entries[3].Name);
                Assert.Equal("tack", entries[4].Name);
                Assert.Equal("blow", entries[5].Name);
                Assert.Equal("game", entries[6].Name);
            });

        [Fact]
        public void IsReadingEntrySizesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(40, entries[0].Stream.Length);
                Assert.Equal(40, entries[1].Stream.Length);
                Assert.Equal(48, entries[2].Stream.Length);
                Assert.Equal(40, entries[3].Stream.Length);
                Assert.Equal(56, entries[4].Stream.Length);
                Assert.Equal(64, entries[5].Stream.Length);
                Assert.Equal(56, entries[6].Stream.Length);
            });

        [Fact]
        public void IsReadingEntryTypesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(Bar.EntryType.Vibration, entries[0].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[1].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[2].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[3].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[4].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[5].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[6].Type);
            });

        [Fact]
        public void IsReadingEntryIndexesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(0, entries[0].Index);
                Assert.Equal(0, entries[1].Index);
                Assert.Equal(0, entries[2].Index);
                Assert.Equal(0, entries[3].Index);
                Assert.Equal(0, entries[4].Index);
                Assert.Equal(0, entries[5].Index);
                Assert.Equal(0, entries[6].Index);
            });

        [Fact]
        public void IsReadingEntryDataCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(86, entries[0].Stream.ReadByte());
                Assert.Equal(86, entries[1].Stream.ReadByte());
                Assert.Equal(86, entries[2].Stream.ReadByte());
                Assert.Equal(86, entries[3].Stream.ReadByte());
                Assert.Equal(86, entries[4].Stream.ReadByte());
                Assert.Equal(86, entries[5].Stream.ReadByte());
                Assert.Equal(86, entries[6].Stream.ReadByte());
            });

        private void FileOpenRead(string path, Action<Stream> action)
        {
            using (var stream = File.OpenRead(path))
                action(stream);
        }
    }
}<commit_msg>Add tests for writing back a BAR<commit_after>﻿using kh.kh2;
using System;
using System.IO;
using System.Linq;
using Xunit;

namespace kh.tests.kh2
{
    public class BarTests
    {
        private const string FilePath = "kh2/res/bar_test.bar";

        [Fact]
        public void IsValidTest()
        {
            using (var stream = new MemoryStream())
            {
                stream.WriteByte(0x42);
                stream.WriteByte(0x41);
                stream.WriteByte(0x52);
                stream.WriteByte(0x01);
                stream.Position = 0;
                Assert.True(Bar.IsValid(stream));
            }
        }

        [Fact]
        public void IsNotValidTest()
        {
            using (var stream = new MemoryStream())
            {
                stream.WriteByte(1);
                stream.WriteByte(2);
                stream.WriteByte(3);
                stream.WriteByte(4);
                stream.Position = 0;
                Assert.False(Bar.IsValid(stream));
            }
        }

        [Fact]
        public void IsReadingEntryCountCorrect() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream);
                Assert.Equal(7, entries.Count());
            });

        [Fact]
        public void IsReadingEntryNamesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal("s_da", entries[0].Name);
                Assert.Equal("m_da", entries[1].Name);
                Assert.Equal("l_da", entries[2].Name);
                Assert.Equal("thru", entries[3].Name);
                Assert.Equal("tack", entries[4].Name);
                Assert.Equal("blow", entries[5].Name);
                Assert.Equal("game", entries[6].Name);
            });

        [Fact]
        public void IsReadingEntrySizesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(40, entries[0].Stream.Length);
                Assert.Equal(40, entries[1].Stream.Length);
                Assert.Equal(48, entries[2].Stream.Length);
                Assert.Equal(40, entries[3].Stream.Length);
                Assert.Equal(56, entries[4].Stream.Length);
                Assert.Equal(64, entries[5].Stream.Length);
                Assert.Equal(56, entries[6].Stream.Length);
            });

        [Fact]
        public void IsReadingEntryTypesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(Bar.EntryType.Vibration, entries[0].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[1].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[2].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[3].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[4].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[5].Type);
                Assert.Equal(Bar.EntryType.Vibration, entries[6].Type);
            });

        [Fact]
        public void IsReadingEntryIndexesCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(0, entries[0].Index);
                Assert.Equal(0, entries[1].Index);
                Assert.Equal(0, entries[2].Index);
                Assert.Equal(0, entries[3].Index);
                Assert.Equal(0, entries[4].Index);
                Assert.Equal(0, entries[5].Index);
                Assert.Equal(0, entries[6].Index);
            });

        [Fact]
        public void IsReadingEntryDataCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var entries = Bar.Open(stream)
                    .ToArray();
                Assert.Equal(86, entries[0].Stream.ReadByte());
                Assert.Equal(86, entries[1].Stream.ReadByte());
                Assert.Equal(86, entries[2].Stream.ReadByte());
                Assert.Equal(86, entries[3].Stream.ReadByte());
                Assert.Equal(86, entries[4].Stream.ReadByte());
                Assert.Equal(86, entries[5].Stream.ReadByte());
                Assert.Equal(86, entries[6].Stream.ReadByte());
            });

        [Fact]
        public void IsWritingBackCorrectly() =>
            FileOpenRead(FilePath, stream =>
            {
                var expectedData = new byte[stream.Length];
                stream.Read(expectedData, 0, expectedData.Length);

                var entries = Bar.Open(new MemoryStream(expectedData));
                using (var dstStream = new MemoryStream(expectedData.Length))
                {
                    Bar.Save(dstStream, entries);
                    dstStream.Position = 0;
                    var actualData = new byte[dstStream.Length];
                    dstStream.Read(actualData, 0, actualData.Length);

                    Assert.Equal(expectedData.Length, actualData.Length);
                    Assert.Equal(expectedData, actualData);
                }
            });

        private void FileOpenRead(string path, Action<Stream> action)
        {
            using (var stream = File.OpenRead(path))
                action(stream);
        }
    }
}
{'fraction_non_alphanumeric': 0.12347740667976424, 'fraction_numerical': 0.01836935166994106, 'mean_word_length': 1.4941205291523763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6477542', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2588, 'n_words': 431}