public void ReadTextLinesTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string[] stlines = { "This is line1", "This is line2", "This is line3" }; Encoding encoding = Encoding.UTF32; System.IO.File.WriteAllLines(filePath, stlines, encoding); List <string> expected = new List <string>(); expected.AddRange(stlines); List <string> actual; actual = target.ReadTextLines(encoding); if (expected.Count != actual.Count) { Assert.Fail("No of Lines not equal"); } int i = 0; foreach (string l in expected) { if (l != actual[i++]) { Assert.Fail("String line not equal"); } } }
/// <summary> /// Writes workplane to a MATRIX file. /// </summary> /// <param name="matrixFile">The file path to the '.matrix' file.</param> public void WriteToMatrix(File matrixFile) { matrixFile.Delete(); // The expected format of the transformation matrix is the following: //xAxis.i|yAxis.i|zAxis.i|x //xAxis.j|yAxis.j|zAxis.j|y //xAxis.k|yAxis.k|zAxis.k|z //0|0|0|1 // The 1st column is the new x vector relative to World // The 2nd column is the new y vector relative to World // The 3rd column is the new z vector relative to World // The 4th column is the new origin position relative to World string firstTranformationMatrixLine = $"{XAxis.I}|{YAxis.I}|{ZAxis.I}|{Origin.X}"; string secondTranformationMatrixLine = $"{XAxis.J}|{YAxis.J}|{ZAxis.J}|{Origin.Y}"; string thirdTranformationMatrixLine = $"{XAxis.K}|{YAxis.K}|{ZAxis.K}|{Origin.Z}"; string fourthTranformationMatrixLine = "0|0|0|1"; var lines = new List <string>(6); lines.Add(firstTranformationMatrixLine); lines.Add(secondTranformationMatrixLine); lines.Add(thirdTranformationMatrixLine); lines.Add(fourthTranformationMatrixLine); matrixFile.WriteTextLines(lines); }
public void MoveToDirectoryTest() { File file = new File(Path.GetTempFileName()); string filepathBeforeMove = file.Path; string filename = Path.GetFileName(filepathBeforeMove); string destinationDirName = "testdir"; Directory destinationDir = new Directory(Path.Combine(_testDirectory, destinationDirName)); if (destinationDir.Exists) { System.IO.Directory.Delete(destinationDir.Path, true); } System.IO.Directory.CreateDirectory(destinationDir.Path); string destinationDirPath = destinationDir.Path; if (destinationDirPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { destinationDirPath = destinationDirPath.Remove(destinationDirPath.Length - 1, 1); } file.MoveToDirectory(destinationDir); Assert.AreEqual(1, System.IO.Directory.GetFiles(destinationDir.Path) .Count(x => Path.GetFileName(x) == filename), "File should have been relocated."); Assert.AreEqual(destinationDirPath, Path.GetDirectoryName(file.Path), "File should have updated its location."); Assert.IsFalse(System.IO.File.Exists(filepathBeforeMove), "File should not exist at original location."); }
public void MoveTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); string newDir = Path.Combine(_testDirectory, "testDir"); string newFile = Path.Combine(newDir, fname); File target = new File(filePath); if (!System.IO.Directory.Exists(newDir)) { System.IO.Directory.CreateDirectory(newDir); } target.Create(); System.IO.File.Delete(newFile); target.Move(new File(newFile)); Assert.IsTrue(System.IO.File.Exists(newFile), "File has not been moved"); //check pathname is correct Assert.AreEqual(newFile, target.Path, "Filename has not been updated internally"); // And parent directory too Assert.That(target.ParentDirectory.Path, Is.EqualTo(newDir + "\\")); System.IO.File.Delete(newFile); }
/// <summary> /// Writes the transformation matrix to a mat file. /// </summary> /// <param name="matFile">The MAT file to write.</param> /// <param name="rotationMatrix">The rotation matrix.</param> /// <param name="translation">The translation matrix.</param> private void WriteRotationMatrixAndTranslationToMatFile(File matFile, double[,] rotationMatrix, double[] translation) { // Delete the file if a previous one exists if (System.IO.File.Exists(matFile.Path)) { System.IO.File.Delete(matFile.Path); } string xLine = null; string yLine = null; var zLine = ""; // PowerInspect writes the MAT files using Invariant culture. string transLine = " T " + Convert.ToString(translation[0], CultureInfo.InvariantCulture) + " " + Convert.ToString(translation[1], CultureInfo.InvariantCulture) + " " + Convert.ToString(translation[2], CultureInfo.InvariantCulture); var rotations = Angles.GetRotationsFromMatrixXYZ(rotationMatrix); zLine = " R Z " + Convert.ToString(rotations[0].Value, CultureInfo.InvariantCulture); yLine = " R Y " + Convert.ToString(rotations[1].Value, CultureInfo.InvariantCulture); xLine = " R X " + Convert.ToString(rotations[2].Value, CultureInfo.InvariantCulture); var lines = new List <string>(); lines.Add(xLine); lines.Add(yLine); lines.Add(zLine); lines.Add(transLine); lines.Add("*"); lines.Add(""); // create a new text document System.IO.File.WriteAllLines(matFile.Path, lines); }
public void PathTest() { string filePath = Path.Combine(_testDirectory, "TestABC.txt"); File target = new File(Path.Combine(_testDirectory, "TestABC.txt")); Assert.AreEqual(filePath, target.Path, "Filepath is incorrect."); }
public void ExtensionTest() { File target = new File(Path.Combine(_testDirectory, "test123.txt")); Assert.AreEqual("txt", target.Extension, "File extension should be txt."); target = new File(Path.Combine(_testDirectory, "test123")); Assert.AreEqual("", target.Extension, "File should have no extension."); }
public void WriteTextTest2() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string line1 = "This is Line 1"; System.IO.File.Delete(filePath); target.WriteText(line1, false, Encoding.UTF8); string[] fileLines = System.IO.File.ReadAllLines(filePath); if (fileLines == null) { Assert.Fail("Could not retrieve text file lines"); } else if (fileLines.Length != 1) { Assert.Fail("Expecting one line"); } else if (fileLines[0] != line1) { Assert.Fail("Read 1st line not equal to expected line"); } else { // ok now append line string[] lines = new string[2]; string line2 = "This is second line"; target.WriteText(line2, true, Encoding.UTF8); // should put this on 1st line/ fileLines = System.IO.File.ReadAllLines(filePath); lines[0] = line1; lines[1] = line2; //int i = 0; Assert.IsNotNull(fileLines, "Could not retrieve file lines"); Assert.AreEqual(1, fileLines.Length, "Should only be 1 line"); string linecomb = line1 + line2; Assert.AreEqual(linecomb, fileLines[0], "Should be one 1 line"); /* * foreach(string s in fileLines) * { * * Assert.AreEqual(lines[i],s,string.Format("Line {0} not equal",i)); * i++; * } * */ } }
public void FileConstructorTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); Directory parentDirectory = new Directory(Path.GetDirectoryName(filePath)); File target = new File(parentDirectory, fname); Assert.AreEqual(filePath, target.Path, "Expected same filepath"); }
public void CreateTest() { string filePath = Path.Combine(_testDirectory, "test123.txt"); System.IO.File.Delete(filePath); File target = new File(filePath); target.Create(); Assert.IsTrue(System.IO.File.Exists(filePath), "File should exist"); }
public void ToStringTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string expected = filePath; string actual; actual = target.ToString(); Assert.AreEqual(expected, actual, "Values should be equal"); }
public void NameWithoutExtensionTest() { File file = new File(Path.Combine(_testDirectory, "TestFile.txt")); Assert.AreEqual("TestFile", file.NameWithoutExtension, "Short name should be 'TestFile'"); file = new File(Path.Combine(_testDirectory, "TestFile2")); Assert.AreEqual("TestFile2", file.NameWithoutExtension, "Short name should be 'TestFile2'"); file = new File(Path.Combine(_testDirectory, "TestFile3.txt.tmp")); Assert.AreEqual("TestFile3.txt", file.NameWithoutExtension, "Short name should be 'TestFile3.txt'"); }
public void ParentDirectoryTest() { File target = new File(@"c:\abcdefg\test1.txt"); Assert.AreEqual(@"c:\abcdefg\", target.ParentDirectory.Path, "Parent directory of file is wrong."); target = new File(@"c:\test2.txt"); Assert.AreEqual(@"c:\", target.ParentDirectory.Path, "Parent directory of file is wrong."); target = new File(@"c:\dir1\dir2\test2.txt"); Assert.AreEqual(@"c:\dir1\dir2\", target.ParentDirectory.Path, "Parent directory of file is wrong."); }
public void ReadTextLinesTestWhenFileBeingAccessedByAnotherProcess() { string fname = "ReadTextLinesTest.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); var task = new Task(() => UseFileStream3Second(filePath)); task.Start(); Delay(200).Wait(); // Let the task run and acquire the file lock Assert.DoesNotThrow(() => target.ReadTextLines()); }
public void DeleteTest() { string filePath = Path.Combine(_testDirectory, "test123.txt"); if (!System.IO.File.Exists(filePath)) { System.IO.File.Create(filePath).Dispose(); } File target = new File(filePath); target.Delete(); Assert.IsFalse(System.IO.File.Exists(target.Path), "File should not exist"); }
public void LastChangedDateTest() { File file = new File(Path.Combine(_testDirectory, "TestLastChangedDate.Txt")); file.WriteText("testing"); DateTime time1 = new FileInfo(file.Path).LastWriteTime; Thread.Sleep(100); file.WriteText("more text"); DateTime time2 = new FileInfo(file.Path).LastWriteTime; Assert.AreNotEqual(time1, time2, "File modified datetimes should be different."); }
public void ExistsTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); System.IO.File.Delete(filePath); Assert.IsFalse(target.Exists, "File should not exist"); //now create file System.IO.File.WriteAllText(filePath, "Test"); Assert.IsTrue(target.Exists, "File should exist"); }
public void FileConstructorTest1() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); try { File target = new File(filePath); } catch (Exception e) { Assert.Fail("Constructor Failed: {0}", e); } }
public void ReadTextTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string expected = "Single string"; System.IO.File.WriteAllText(filePath, expected); string actual = target.ReadText(); Assert.AreEqual(expected, actual, "String should be equal"); }
public void CopyToDirectoryTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File source = new File(filePath); source.Create(); File destination = new File(new Directory(Path.Combine(_testDirectory, "CopyToDirectory")), fname); destination.ParentDirectory.Delete(); destination.ParentDirectory.Create(); source.CopyToDirectory(destination.ParentDirectory); Assert.AreEqual(true, destination.Exists); }
public void RenameTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); target.Create(); string fname2 = "test123d.txt"; string filePath2 = Path.Combine(_testDirectory, fname2); target.Rename(filePath2); Assert.IsTrue(System.IO.File.Exists(filePath2), "File should exist"); Assert.IsFalse(System.IO.File.Exists(filePath), "File should not exist"); Assert.AreEqual(target.Path, filePath2, "File name should be equal"); }
public void WhenWritingSTLFileFromDMTFile_ThenCheckOutput() { DMTModel importedModel = DMTModelReader.ReadFile(new File(TestFiles.NormalDmt)); var outputFile = new File(string.Format("{0}\\output.stl", Path.GetTempPath())); DMTModelWriter.WriteFile(importedModel, outputFile); DMTModel writtenModel = DMTModelReader.ReadFile(outputFile); // Ensure that model is written correctly Assert.AreEqual(importedModel.BoundingBox.MaxX, writtenModel.BoundingBox.MaxX); Assert.AreEqual(importedModel.BoundingBox.MaxY, writtenModel.BoundingBox.MaxY); Assert.AreEqual(importedModel.BoundingBox.MaxZ, writtenModel.BoundingBox.MaxZ); Assert.AreEqual(importedModel.BoundingBox.MinX, writtenModel.BoundingBox.MinX); Assert.AreEqual(importedModel.BoundingBox.MinY, writtenModel.BoundingBox.MinY); Assert.AreEqual(importedModel.BoundingBox.MinZ, writtenModel.BoundingBox.MinZ); outputFile.Delete(); }
public void CreateTemporaryFileTest() { string extension = ".tmp"; File actual; actual = File.CreateTemporaryFile(extension); Assert.IsFalse(System.IO.File.Exists(actual.Path), "File should not exist"); actual.Create(); Assert.IsTrue(System.IO.File.Exists(actual.Path), "File should exist"); string dir = Path.GetDirectoryName(actual.Path) + "\\"; Assert.AreEqual(Path.GetTempPath(), dir, "Temp Directory different"); actual.Delete(); Assert.IsFalse(System.IO.File.Exists(actual.Path), "Temp File should not exist"); }
/// <summary> /// Writes the transformation matrix to a TRX file. /// </summary> /// <param name="trxFile">The TRX file to write.</param> /// <param name="rotationMatrix">The rotation matrix.</param> /// <param name="translation">The translation matrix.</param> private void WriteRotationMatrixAndTranslationToTrxFile( File trxFile, double[,] rotationMatrix, double[] translation) { trxFile.Delete(); Degree[] rotations = Angles.GetRotationsFromMatrixXYZ(rotationMatrix); string xRotationLine = "ROT_X: " + (rotations[2].Value >= 0.0 ? rotations[2].Value.ToString("+0.000") : rotations[2].Value.ToString("0.000")); string yRotationLine = "ROT_Y: " + (rotations[1].Value >= 0.0 ? rotations[1].Value.ToString("+0.000") : rotations[1].Value.ToString("0.000")); string zRotationLine = "ROT_Z: " + (rotations[0].Value >= 0.0 ? rotations[0].Value.ToString("+0.000") : rotations[0].Value.ToString("0.000")); string xTranslationLine = "SHIFT_X: " + (translation[0] >= 0.0 ? translation[0].ToString("+0.000") : translation[0].ToString("0.000")); string yTranslationLine = "SHIFT_Y: " + (translation[1] >= 0.0 ? translation[1].ToString("+0.000") : translation[1].ToString("0.000")); string zTranslationLine = "SHIFT_Z: " + (translation[0] >= 0.0 ? translation[2].ToString("+0.000") : translation[2].ToString("0.000")); var lines = new List <string>(6); lines.Add(xRotationLine); lines.Add(yRotationLine); lines.Add(zRotationLine); lines.Add(xTranslationLine); lines.Add(yTranslationLine); lines.Add(zTranslationLine); trxFile.WriteTextLines(lines); }
public void WriteTextTest() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string line1 = "This is Line 1"; System.IO.File.Delete(filePath); bool appendText = false; target.WriteText(line1, appendText); target.WriteText(line1, true); string combLine = line1 + line1; string linein = System.IO.File.ReadAllText(filePath); Assert.AreEqual(combLine, linein, "Lines should be equal"); }
/// <summary> /// Creates workplane from MAT, a TRX or a MATRIX file. /// </summary> /// <param name="file">The file to create the workplane from.</param> public Workplane(File file) { Tuple <double[, ], double[]> rotationMatrixAndTranslation; if (IsAMatFile(file)) { rotationMatrixAndTranslation = GetRotationMatrixAndTranslationFromMatFile(file); } else if (IsATrxFile(file)) { rotationMatrixAndTranslation = GetRotationMatrixAndTranslationFromTrxFile(file); } else if (IsAMatrixFile(file)) { rotationMatrixAndTranslation = GetRotationMatrixAndTranslationFromMatrixFile(file); } else { throw new ArgumentException("File type is invalid, a MAT file, a TRX file or a MATRIX file is expected."); } // Get the origin of the workplane _origin = new Point(rotationMatrixAndTranslation.Item2[0], rotationMatrixAndTranslation.Item2[1], rotationMatrixAndTranslation.Item2[2]); // Get the x, y and z axis of the workplane _xAxis = new Vector(rotationMatrixAndTranslation.Item1[0, 0], rotationMatrixAndTranslation.Item1[1, 0], rotationMatrixAndTranslation.Item1[2, 0]); _yAxis = new Vector(rotationMatrixAndTranslation.Item1[0, 1], rotationMatrixAndTranslation.Item1[1, 1], rotationMatrixAndTranslation.Item1[2, 1]); _zAxis = new Vector(rotationMatrixAndTranslation.Item1[0, 2], rotationMatrixAndTranslation.Item1[1, 2], rotationMatrixAndTranslation.Item1[2, 2]); }
public void WriteTextTest1() { string fname = "test123.txt"; string filePath = Path.Combine(_testDirectory, fname); File target = new File(filePath); string line1 = "This is Line 1"; System.IO.File.Delete(filePath); target.WriteText(line1); string linein = System.IO.File.ReadAllText(filePath); Assert.AreEqual(line1, linein, "Lines should be equal"); string line2 = "This is line 2"; target.WriteText(line2); // should not append linein = System.IO.File.ReadAllText(filePath); Assert.AreEqual(line2, linein, "Lines should be equal"); }
public void CopyTest() { string fileContents = "testing"; File file = CreateFileInTempDirectory(); System.IO.File.WriteAllText(file.Path, fileContents); // Test that copying to an unused location succeeds. File destination = CreateFileInTempDirectory(); file.Copy(destination); Assert.IsTrue(System.IO.File.Exists(destination.Path), "File should exist"); Assert.AreEqual(fileContents, System.IO.File.ReadAllLines(destination.Path)[0], "File contents should have been copied."); // Test that copying to an existing location succeeds. System.IO.File.Delete(destination.Path); System.IO.File.Create(destination.Path).Dispose(); file.Copy(destination); Assert.AreEqual(fileContents, System.IO.File.ReadAllLines(destination.Path)[0], "File contents should have been copied."); }
/// <summary> /// Checks if it is a TRX file. /// </summary> /// <param name="file">The file to be tested.</param> /// <returns>True if it is a TRX file, false otherwise.</returns> private bool IsATrxFile(File file) { return(file.Extension.ToLowerInvariant() == "trx"); }
/// <summary> /// Checks if it is a Matrix file. A file ending with '.matrix' is a custom file format created to be able to load a workplane from a file describing a transformation matrix. /// </summary> /// <param name="file">File with the transformation matrix.</param> /// <returns>True if it is a Matrix file, false otherwise.</returns> private bool IsAMatrixFile(File file) { return(file.Extension.ToLowerInvariant() == "matrix"); }