private static void TestIEnumerator()
        {
            String chkptFlag = "chkpt_ienum_";
            int    failCount = 0;

            IEnumerable <String> dirs_temp = Directory.EnumerateDirectories(s_utils.testDir, "*", SearchOption.AllDirectories);
            IEnumerator          dirs      = dirs_temp.GetEnumerator();

            try
            {
                if (!dirs.MoveNext())
                {
                    failCount++;
                    Console.WriteLine(chkptFlag + "1: MoveNext returned false. Expected some contents");
                }
                if (dirs.Current == null)
                {
                    failCount++;
                    Console.WriteLine(chkptFlag + "2: Current returned null. Expected some contents");
                }

                // Reset should throw
                dirs.Reset();
                failCount++;
                Console.WriteLine(chkptFlag + "3: Reset didn't throw exception. It should have thrown NotSupportedException");
            }
            catch (NotSupportedException)
            {
            }
            catch (Exception e)
            {
                failCount++;
                Console.WriteLine(chkptFlag + "4: Unexpected exception...");
                Console.WriteLine(e);
            }
            finally
            {
                ((IEnumerator <String>)dirs).Dispose();  // if we jump out while enumerating, the enumerator still holds the file handle
            }


            string testname = "IEnumerator";

            s_utils.PrintTestStatus(testname, "EnumerateDirectories", failCount);
        }
Beispiel #2
0
        private static void DoDirectoryGetDirectoriesTest(String path, String searchPattern, SearchOption searchOption, HashSet <FSIEntry> expected)
        {
            String chkptFlag = "chkpt_dgd1_";
            int    failCount = 0;

            // directoryinfo
            DirectoryInfo di = new DirectoryInfo(path);

            DirectoryInfo[]    dis     = di.GetDirectories(searchPattern, searchOption);
            HashSet <FSIEntry> disAsHS = new HashSet <FSIEntry>();

            foreach (DirectoryInfo d in dis)
            {
                disAsHS.Add(new FSIEntry(d.Name, d.FullName, null, d.ToString()));
            }

            if (!disAsHS.SetEquals(expected))
            {
                failCount++;
                Console.WriteLine(chkptFlag + "1: didn't get expected directories....");
                Console.WriteLine("Expected:");
                foreach (FSIEntry d in expected)
                {
                    Console.WriteLine("   Name:       " + d.Name);
                    Console.WriteLine("   FullName:   " + d.FullName);
                    Console.WriteLine("   ToString(): " + d.ToStr);
                }
                Console.WriteLine("But got:");
                foreach (FSIEntry d in disAsHS)
                {
                    Console.WriteLine("   Name        " + d.Name);
                    Console.WriteLine("   FullName:   " + d.FullName);
                    Console.WriteLine("   ToString(): " + d.ToStr);
                }
            }

            String testName = String.Format("SP={0}, SO={1}", searchPattern, searchOption);

            s_utils.PrintTestStatus(testName, "DirectoryInfo.GetDirectories", failCount);
        }
Beispiel #3
0
        private static void TestFileWriteAllLines(bool useEncodingOverload, bool append)
        {
            String chkptFlag = "chkpt_wal_";
            int    failCount = 0;

            String encodingPart = useEncodingOverload ? ", Encoding" : "";
            String writePart    = append ? "Append" : "Write";
            String testname     = String.Format("File.{0}AllLines(path{1})", writePart, encodingPart);


            String lineContent      = "This is another line";
            String firstLineContent = "This is the first line";

            String[] contents = new String[10];
            for (int i = 0; i < contents.Length; i++)
            {
                contents[i] = lineContent;
            }

            String file1 = Path.Combine(utils.testDir, "file1.out");

            if (append)
            {
                File.WriteAllLines(file1, new String[] { firstLineContent });
            }


            if (useEncodingOverload)
            {
                if (append)
                {
                    File.AppendAllLines(file1, (IEnumerable <String>)contents, Encoding.UTF8);
                }
                else
                {
                    File.WriteAllLines(file1, (IEnumerable <String>)contents, Encoding.UTF8);
                }
            }
            else
            {
                if (append)
                {
                    File.AppendAllLines(file1, (IEnumerable <String>)contents);
                }
                else
                {
                    File.WriteAllLines(file1, (IEnumerable <String>)contents);
                }
            }


            int expectedCount = append ? 11 : 10;

            IEnumerable <String> fileContents = null;

            if (useEncodingOverload)
            {
                fileContents = File.ReadLines(file1, Encoding.UTF8);
            }
            else
            {
                fileContents = File.ReadLines(file1);
            }

            int count = 0;

            foreach (String line in fileContents)
            {
                if (append && count == 0)
                {
                    if (!firstLineContent.Equals(line))
                    {
                        failCount++;
                        Console.WriteLine(chkptFlag + "1: {0} FAILED at first line", testname);
                        Console.WriteLine("got unexpected line: " + line);
                    }
                    count++;
                    continue;
                }

                count++;
                if (!lineContent.Equals(line))
                {
                    failCount++;
                    Console.WriteLine(chkptFlag + "2: {0} FAILED", testname);
                    Console.WriteLine("got unexpected line: " + line);
                }
            }
            if (count != expectedCount)
            {
                failCount++;
                Console.WriteLine(chkptFlag + "3: {0} FAILED. Line count didn't equal expected", testname);
                Console.WriteLine("expected {0} but got {1}", expectedCount, count);
            }

            utils.PrintTestStatus(testname, "WriteAllLines", failCount);

            File.Delete(file1);
        }
        private static void DoDirectoryGetDirectoriesTest(String path, String searchPattern, SearchOption searchOption, HashSet <String> expected)
        {
            String chkptFlag = "chkpt_dgd1_";

            IEnumerable <String> dirs     = Directory.EnumerateDirectories(path, searchPattern, searchOption);
            HashSet <String>     dirsAsHS = new HashSet <string>(dirs);
            int failCount = 0;

            if (!dirsAsHS.SetEquals(expected))
            {
                failCount++;
                Console.WriteLine(chkptFlag + "1: didn't get expected directories....");
                foreach (String d in dirs)
                {
                    Console.WriteLine(d);
                }
            }

            String testName = String.Format("SP={0}, SO={1}", searchPattern, searchOption);

            s_utils.PrintTestStatus(testName, "Directory.EnumerateDirectories", failCount);
        }