예제 #1
0
        public void NumberListColumn_Basics()
        {
            NumberListColumn <int> column = new NumberListColumn <int>();

            column[0].SetTo(new ArraySlice <int>(new int[] { 0, 1, 2 }));

            TreeDiagnostics diagnostics      = TreeSerializer.Diagnostics(column, TreeFormat.Binary);
            int             tinyColumnLength = (int)diagnostics.Length;

            Column.Basics(() => new NumberListColumn <int>(), NumberList <int> .Empty, column[0], (index) =>
            {
                column[index].SetTo(new ArraySlice <int>(new int[] { index, index + 1, index + 2 }));
                return(column[index]);
            });

            // NumberList Equals, GetHashCode
            int[] asArray = new int[] { 0, 1, 3 };
            column[1].SetTo(new ArraySlice <int>(asArray));
            Assert.False(column[0] == column[1]);
            Assert.True(column[0] != column[1]);
            Assert.NotEqual(column[0].GetHashCode(), column[1].GetHashCode());

            // Compare to array
            Assert.True(column[1].Equals(asArray));
        }
예제 #2
0
        public void Database_Basics()
        {
            V1.Community community = new V1.Community();
            community.People = new List <V1.Person>();

            community.People.Add(new V1.Person()
            {
                Age = 39, Name = "Scott"
            });
            community.People.Add(new V1.Person()
            {
                Age = 36, Name = "Adam"
            });

            // Use ReadOnlyList.VerifySame to check count, enumerators, and indexer
            community.DB.Save("V1.Community.bsoa", TreeFormat.Binary);
            V1.Community roundTripped = new V1.Community();
            roundTripped.DB.Load("V1.Community.bsoa", TreeFormat.Binary);
            CollectionReadVerifier.VerifySame(community.People, roundTripped.People);

            // Try loading database with size diagnostics
            TreeDiagnostics diagnostics = TreeSerializer.Diagnostics(community.DB, () => new V1.Community().DB, TreeFormat.Binary);

            // Verify table and column names in diagnostics
            string text = diagnostics.ToString();

            Assert.Contains("Person", text);
            Assert.Contains("Age", text);
            Assert.Contains("Name", text);

            // Verify Person has two columns, Write doesn't throw
            Assert.Equal("Person", diagnostics.Children[0].Name);
            Assert.Equal(Names.Columns, diagnostics.Children[0].Children[0].Name);
            Assert.Equal(2, diagnostics.Children[0].Children[0].Children.Count);
            diagnostics.Write(Console.Out, 3);

            // Verify Trim doesn't throw (results not visible)
            community.DB.Trim();
            CollectionReadVerifier.VerifySame(community.People, roundTripped.People);

            // Verify Copy constructor recursively copies (List.SetTo -> LocalIndex -> CopyFrom construction)
            V1.Community copy = new V1.Community(community);
            CollectionReadVerifier.VerifySame(community.People, copy.People);
            community.People[0].Age += 10;
            Assert.NotEqual(community.People[0].Age, copy.People[0].Age);

            // Verify Database.Clear works
            community.DB.Clear();
            var people = community.People;

            Assert.True(people == null || people.Count == 0);
        }
예제 #3
0
        public void BooleanColumn_AllDefault()
        {
            BooleanColumn c = new BooleanColumn(true);

            // Set a large number of values all to the default
            for (int i = 0; i < 8192; ++i)
            {
                c[i] = true;
            }

            // Verify the column serializes small (not to one bit per row)
            TreeDiagnostics diagnostics = TreeSerializer.Diagnostics(c, () => new BooleanColumn(true), TreeFormat.Binary);

            Assert.True(diagnostics.Length < 100);
        }
예제 #4
0
        public void NumberListColumn_Basics()
        {
            NumberListColumn <int> column = new NumberListColumn <int>();

            column[0].SetTo(new ArraySlice <int>(new int[] { 0, 1, 2 }));

            TreeDiagnostics diagnostics      = TreeSerializer.Diagnostics(column, TreeFormat.Binary);
            int             tinyColumnLength = (int)diagnostics.Length;

            Column.Basics(() => new NumberListColumn <int>(), NumberList <int> .Empty, column[0], (index) =>
            {
                column[index].SetTo(new ArraySlice <int>(new int[] { index, index + 1, index + 2 }));
                return(column[index]);
            });

            // NumberList Equals, GetHashCode
            int[] asArray = new int[] { 0, 1, 3 };
            column[1].SetTo(new ArraySlice <int>(asArray));
            Assert.False(column[0] == column[1]);
            Assert.True(column[0] != column[1]);
            Assert.NotEqual(column[0].GetHashCode(), column[1].GetHashCode());

            // Compare to array
            Assert.True(column[1].Equals(asArray));

            // ForEach
            column.Clear();
            column[0].SetTo(new ArraySlice <int>(new int[] { 0, 1, 2 }));
            int sum = 0;

            column.ForEach((slice) =>
            {
                int[] array = slice.Array;
                int end     = slice.Index + slice.Count;
                for (int i = slice.Index; i < end; ++i)
                {
                    sum += array[i];
                }
            });
            Assert.Equal(3, sum);
        }
예제 #5
0
        public void GenericNumberListColumn_Basics()
        {
            List <int> empty = new List <int>();

            GenericNumberListColumn <int> column = new GenericNumberListColumn <int>();

            column[0] = new int[] { 0, 1, 2 };

            TreeDiagnostics diagnostics      = TreeSerializer.Diagnostics(column, TreeFormat.Binary);
            int             tinyColumnLength = (int)diagnostics.Length;

            Column.Basics(() => new GenericNumberListColumn <int>(), null, column[0], (index) =>
            {
                IList <int> values = column[index];
                if (values == null || values.Count == 0)
                {
                    column[index] = new int[] { index, index + 1, index + 2 };
                    values        = column[index];
                }

                return(values);
            });

            // ForEach
            column.Clear();
            column[0] = new int[] { 0, 1, 2 };
            int sum = 0;

            column.ForEach((slice) =>
            {
                int[] array = slice.Array;
                int end     = slice.Index + slice.Count;
                for (int i = slice.Index; i < end; ++i)
                {
                    sum += array[i];
                }
            });
            Assert.Equal(3, sum);
        }
예제 #6
0
        public static void Basics(TreeFormat format)
        {
            Random r = new Random();

            // Test integers with specific values (for varying length encodings)
            TestIntegers(format);

            // Test serialization of each primitive value type (bool, string, long, double)
            Sample sample = new Sample(new Random());

            sample.AssertEqual(RoundTrip <Sample>(sample, format));

            // Test serialization of containers (read must leave last token of nested items so loop finds next property name properly)
            SingleContainer <Sample> container = new SingleContainer <Sample>(sample);

            container.AssertEqual(RoundTrip <SingleContainer <Sample> >(container, format));

            // Test diagnostics doesn't throw when over Reader
            TreeDiagnostics diagnostics = Diagnostics(sample, format);

            // Test serialization of all supported primitive array types
            RoundTripArray(new byte[] { 0, 4, 16 }, format);
            RoundTripArray(new char[] { 'S', 'o', 'A' }, format);
            RoundTripArray(new sbyte[] { 0, 4, 16 }, format);
            RoundTripArray(new ushort[] { 0, 4, 16 }, format);
            RoundTripArray(new short[] { 0, 4, 16 }, format);
            RoundTripArray(new uint[] { 0, 4, 16 }, format);
            RoundTripArray(new int[] { 0, 4, 16 }, format);
            RoundTripArray(new ulong[] { 0, 4, 16 }, format);
            RoundTripArray(new long[] { 0, 4, 16 }, format);
            RoundTripArray(new float[] { 0.0f, 100.5f, -2.05f }, format);
            RoundTripArray(new double[] { 0.0f, 100.5f, -2.05f }, format);

            // Verify exception on (expected) unsupported type
            Assert.Throws <NotSupportedException>(() => RoundTripArray(new decimal[] { 0.01M, 0.02M }, format));

            // Null/Empty array handling (currently expected to come back as empty array)
            RoundTripArray <byte>(null, format);
            RoundTripArray <byte>(new byte[] { }, format);

            // Test double Dispose handled correctly, 'Verbose == true' works, 'LeaveStreamOpen' respected
            container.AssertEqual(RoundTrip(container, format, testDoubleDispose: true));
            container.AssertEqual(RoundTrip(container, format, new TreeSerializationSettings()
            {
                LeaveStreamOpen = true
            }));
            container.AssertEqual(RoundTrip(container, format, new TreeSerializationSettings()
            {
                Verbose = true
            }));

            // Test null string handling
            sample.Name = null;
            sample.AssertEqual(RoundTrip(sample, format));

            // Test settings defaulting
            sample.AssertEqual(RoundTrip_NullSettings(sample, format));

            // Test Skip behavior works for each primitive value
            VerifySkip(sample, format);

            // Test serialization details
            using (MemoryStream stream = new MemoryStream())
            {
                TreeSerializationSettings settings = new TreeSerializationSettings()
                {
                    LeaveStreamOpen = true
                };

                using (ITreeWriter writer = Writer(format, stream, settings))
                {
                    sample.Write(writer);
                }

                long bytesWritten = stream.Position;
                stream.Seek(0, SeekOrigin.Begin);

                using (ITreeReader reader = Reader(format, stream, settings))
                {
                    // Test 'Expect' throwing (should not be 'None' when file just opened)
                    Assert.NotEqual(TreeToken.None, reader.TokenType);
                    Assert.Throws <IOException>(() => reader.Expect(TreeToken.None));

                    // Test reading back as wrong type (exception from ReadObject unexpected property name)
                    Assert.Throws <IOException>(() => new SingleContainer <Sample>().Read(reader));
                }
            }
        }