Esempio n. 1
0
        public void IntClusterTableTest()
        {
            ConfigurationTest.Initialize();

            using (var io = ConfigurationTest.CreateMemoryIO()) {
                SimpleClusterIO cio = new SimpleClusterIO(io);

                Random r = new Random();

                ClusterTable <int> ct = new ClusterTable <int>(
                    new int[] { 2, 4 },
                    sizeof(int),
                    (address) => new IntArrayCluster(address)
                {
                    Type = ClusterType.BytesUsedTable
                });

                ClusterTable <int> ct2 = new ClusterTable <int>(
                    new int[] { 2, 4 },
                    sizeof(int),
                    (address) => new IntArrayCluster(address)
                {
                    Type = ClusterType.BytesUsedTable
                });

                ct.Flush(cio);
                ct2.Load(cio);

                // Check that the cluster is written and everything is zeroed

                DataBlock b      = new DataBlock(io.Bytes, 2 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                int       offset = verifyProperties(b, ClusterType.BytesUsedTable, 4);
                for (int i = 0; i < ArrayCluster.CalculateElementCount(sizeof(int)); i++)
                {
                    Assert.AreEqual(b.ToInt32(offset + i * sizeof(int)), 0);
                }

                b      = new DataBlock(io.Bytes, 4 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                offset = verifyProperties(b, ClusterType.BytesUsedTable, Constants.NoAddress);
                for (int i = 0; i < ArrayCluster.CalculateElementCount(sizeof(int)); i++)
                {
                    Assert.AreEqual(b.ToInt32(offset + i * sizeof(int)), 0);
                }

                for (int i = 0; i < ct2.Count; i++)
                {
                    Assert.AreEqual(ct2[i], 0);
                }

                // Now randomize the contents
                for (int i = 0; i < ct.Count; i++)
                {
                    ct[i] = r.Next();
                }
                ct.Flush(cio);

                b      = new DataBlock(io.Bytes, 2 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                offset = verifyProperties(b, ClusterType.BytesUsedTable, 4);
                int index = 0;
                for (int i = 0; i < ArrayCluster.CalculateElementCount(sizeof(int)); i++, index++)
                {
                    Assert.AreEqual(b.ToInt32(offset + i * sizeof(int)), ct[index]);
                }

                b      = new DataBlock(io.Bytes, 4 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                offset = verifyProperties(b, ClusterType.BytesUsedTable, Constants.NoAddress);
                for (int i = 0; i < ArrayCluster.CalculateElementCount(sizeof(int)); i++, index++)
                {
                    Assert.AreEqual(b.ToInt32(offset + i * sizeof(int)), ct[index]);
                }

                ct2.Load(cio);
                for (int i = 0; i < ct2.Count; i++)
                {
                    Assert.AreEqual(ct2[i], ct[i]);
                }

                // Add a cluster
                ct.AddCluster(7);
                ct2.AddCluster(7);
                ct.Flush(cio);

                // Make sure that next cluster is updated
                b = new DataBlock(io.Bytes, 4 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                verifyProperties(b, ClusterType.BytesUsedTable, 7);

                // check the new cluster and assure everything is zero
                b      = new DataBlock(io.Bytes, 7 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                offset = verifyProperties(b, ClusterType.BytesUsedTable, Constants.NoAddress);
                for (int i = 0; i < ArrayCluster.CalculateElementCount(sizeof(int)); i++)
                {
                    Assert.AreEqual(b.ToInt32(offset + i * sizeof(int)), 0);
                }

                ct2.Load(cio);
                for (int i = 0; i < ct2.Count; i++)
                {
                    Assert.AreEqual(ct2[i], ct[i]);
                }

                Assert.AreEqual(ct2.Count, 3 * ArrayCluster.CalculateElementCount(sizeof(int)));

                // Remove a cluster
                ct.RemoveLastCluster();
                ct.Flush(cio);

                // Make sure that the last cluster is updated
                b = new DataBlock(io.Bytes, 4 * Configuration.Geometry.BytesPerCluster, Configuration.Geometry.BytesPerCluster);
                verifyProperties(b, ClusterType.BytesUsedTable, Constants.NoAddress);
            }
        }