public static void ToBinaryTest(string testFilePath, TileSetFileData generatedDat, int fileSize) { var generatedDataBuf = generatedDat.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:X}\"").ToList() .ForEach(Console.WriteLine); Console.WriteLine(); generatedDataBuf.Select((s, index) => $"=\"[{index}] = {{byte}} {s:X}\"").ToList() .ForEach(Console.WriteLine); 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(TileSetFileData resultData, string readFileName) { var filePath = $@"{CommonFileTestItemGenerator.TestWorkRootDir}\{readFileName}"; var reader = new TileSetFileReader(filePath); var readResult = false; TileSetFileData 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(filePath, 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]})"); } } }