Exemple #1
0
        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");
                }
            }
        }
Exemple #2
0
        /// <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);
        }
Exemple #3
0
        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.");
        }
Exemple #4
0
        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);
        }
Exemple #5
0
        /// <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);
        }
Exemple #6
0
        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.");
        }
Exemple #7
0
        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.");
        }
Exemple #8
0
        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++;
                 * }
                 * */
            }
        }
Exemple #9
0
        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");
        }
Exemple #10
0
        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");
        }
Exemple #11
0
        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");
        }
Exemple #12
0
        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'");
        }
Exemple #13
0
        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.");
        }
Exemple #14
0
        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());
        }
Exemple #15
0
        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");
        }
Exemple #16
0
        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.");
        }
Exemple #17
0
        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");
        }
Exemple #18
0
        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);
            }
        }
Exemple #19
0
        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");
        }
Exemple #20
0
        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);
        }
Exemple #21
0
        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");
        }
Exemple #22
0
        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();
        }
Exemple #23
0
        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");
        }
Exemple #24
0
        /// <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);
        }
Exemple #25
0
        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");
        }
Exemple #26
0
        /// <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]);
        }
Exemple #27
0
        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");
        }
Exemple #28
0
        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.");
        }
Exemple #29
0
 /// <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");
 }
Exemple #30
0
 /// <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");
 }