Пример #1
0
        public static void ToBinaryTest(string testFilePath, DatabaseProject generatedData, int fileSize)
        {
            var generatedDataBuf = generatedData.ToBinary();

            using (var fs = new FileStream(testFilePath, FileMode.Open))
            {
                var length = (int)fs.Length;
                // ファイルサイズが規定でない場合誤作動防止の為テスト失敗にする
                Assert.AreEqual(length, fileSize);


                var fileData = new byte[length];
                fs.Read(fileData, 0, length);

                // binデータ出力
                fileData.Select((s, index) => $"=\"[{index}] = {{byte}} {s}\"").ToList()
                .ForEach(s => Console.WriteLine(s));

                Console.WriteLine();

                generatedDataBuf.Select((s, index) => $"=\"[{index}] = {{byte}} {s}\"").ToList()
                .ForEach(s => Console.WriteLine(s));

                for (var i = 0; i < generatedDataBuf.Length; i++)
                {
                    if (i == fileData.Length)
                    {
                        Assert.Fail(
                            $"データ帳が異なります。(期待値:{fileData.Length}, 実際:{generatedDataBuf.Length})");
                    }

                    if (fileData[i] != generatedDataBuf[i])
                    {
                        Assert.Fail(
                            $"offset: {i} のバイナリが異なります。(期待値:{fileData[i]}, 実際:{generatedDataBuf[i]})");
                    }
                }

                if (fileData.Length != generatedDataBuf.Length)
                {
                    Assert.Fail(
                        $"データ帳が異なります。(期待値:{fileData.Length}, 実際:{generatedDataBuf.Length})");
                }
            }
        }
        private static void Common(DatabaseProject resultData, DatabaseProjectFilePath readFileName, DBKind dbKind)
        {
            var reader = new DatabaseProjectFileReader(readFileName, dbKind);

            var             readResult   = false;
            DatabaseProject data         = null;
            var             errorMessage = "";

            try
            {
                data       = reader.ReadSync();
                readResult = true;
            }
            catch (Exception ex)
            {
                logger.Exception(ex);
                errorMessage = ex.Message;
            }


            // 正しく読めること
            if (!readResult)
            {
                throw new InvalidOperationException(
                          $"Error Occured. Message : {errorMessage}");
            }

            Console.WriteLine("Write Test Clear.");

            var readResultDataBytes = data.ToBinary().ToArray();

            // 元のデータと一致すること
            using (var stream = new FileStream(readFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                var bufLength = (int)stream.Length;
                var buf       = new byte[bufLength];
                stream.Read(buf, 0, bufLength);

                if (readResultDataBytes.Length != bufLength)
                {
                    throw new InvalidOperationException(
                              $"Data Length Not Match. " +
                              $"(answerLength: {bufLength}, readResultLength: {readResultDataBytes.Length})");
                }

                for (long i = 0; i < 0; i++)
                {
                    if (readResultDataBytes[i] != buf[i])
                    {
                        throw new InvalidOperationException(
                                  $"Data Byte Not Match. (index: {i}, answer: {buf[i]}," +
                                  $" readResult: {readResultDataBytes[i]})");
                    }
                }
            }

            // 意図したデータと一致すること
            var resultDataBytes = resultData.ToBinary().ToArray();

            if (resultDataBytes.Length != readResultDataBytes.Length)
            {
                throw new InvalidOperationException(
                          $"Data Length Not Match. " +
                          $"(answerLength: {resultDataBytes.Length}, readResultLength: {readResultDataBytes.Length})");
            }

            for (long i = 0; i < 0; i++)
            {
                if (resultDataBytes[i] != readResultDataBytes[i])
                {
                    throw new InvalidOperationException(
                              $"Data Byte Not Match. (index: {i}, answer: {resultDataBytes[i]}," +
                              $" readResult: {readResultDataBytes[i]})");
                }
            }
        }