public virtual void TestAbbreviateIsActuallyUnique() { // This test is far more difficult. We have to manually craft // an input that contains collisions at a particular prefix, // but this is computationally difficult. Instead we force an // index file to have what we want. // ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba"); byte[] idBuf = ToByteArray(id); IList <PackedObjectInfo> objects = new AList <PackedObjectInfo>(); for (int i = 0; i < 256; i++) { idBuf[9] = unchecked ((byte)i); objects.AddItem(new PackedObjectInfo(ObjectId.FromRaw(idBuf))); } string packName = "pack-" + id.Name; FilePath packDir = new FilePath(((ObjectDirectory)db.ObjectDatabase).GetDirectory (), "pack"); FilePath idxFile = new FilePath(packDir, packName + ".idx"); FilePath packFile = new FilePath(packDir, packName + ".pack"); FileUtils.Mkdir(packDir, true); OutputStream dst = new BufferedOutputStream(new FileOutputStream(idxFile)); try { PackIndexWriter writer = new PackIndexWriterV2(dst); writer.Write(objects, new byte[Constants.OBJECT_ID_LENGTH]); } finally { dst.Close(); } new FileOutputStream(packFile).Close(); NUnit.Framework.Assert.AreEqual(id.Abbreviate(20), reader.Abbreviate(id, 2)); AbbreviatedObjectId abbrev8 = id.Abbreviate(8); ICollection <ObjectId> matches = reader.Resolve(abbrev8); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info), "contains " + info.Name); } try { db.Resolve(abbrev8.Name); NUnit.Framework.Assert.Fail("did not throw AmbiguousObjectException"); } catch (AmbiguousObjectException err) { NUnit.Framework.Assert.AreEqual(abbrev8, err.GetAbbreviatedObjectId()); matches = err.GetCandidates(); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info_1 in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info_1), "contains " + info_1.Name ); } } NUnit.Framework.Assert.AreEqual(id, db.Resolve(id.Abbreviate(20).Name)); }
public virtual void TestAbbreviateIsActuallyUnique() { // This test is far more difficult. We have to manually craft // an input that contains collisions at a particular prefix, // but this is computationally difficult. Instead we force an // index file to have what we want. // ObjectId id = Id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba"); byte[] idBuf = ToByteArray(id); IList<PackedObjectInfo> objects = new AList<PackedObjectInfo>(); for (int i = 0; i < 256; i++) { idBuf[9] = unchecked((byte)i); objects.AddItem(new PackedObjectInfo(ObjectId.FromRaw(idBuf))); } string packName = "pack-" + id.Name; FilePath packDir = new FilePath(((ObjectDirectory)db.ObjectDatabase).GetDirectory (), "pack"); FilePath idxFile = new FilePath(packDir, packName + ".idx"); FilePath packFile = new FilePath(packDir, packName + ".pack"); FileUtils.Mkdir(packDir, true); OutputStream dst = new BufferedOutputStream(new FileOutputStream(idxFile)); try { PackIndexWriter writer = new PackIndexWriterV2(dst); writer.Write(objects, new byte[Constants.OBJECT_ID_LENGTH]); } finally { dst.Close(); } new FileOutputStream(packFile).Close(); NUnit.Framework.Assert.AreEqual(id.Abbreviate(20), reader.Abbreviate(id, 2)); AbbreviatedObjectId abbrev8 = id.Abbreviate(8); ICollection<ObjectId> matches = reader.Resolve(abbrev8); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info), "contains " + info.Name); } try { db.Resolve(abbrev8.Name); NUnit.Framework.Assert.Fail("did not throw AmbiguousObjectException"); } catch (AmbiguousObjectException err) { NUnit.Framework.Assert.AreEqual(abbrev8, err.GetAbbreviatedObjectId()); matches = err.GetCandidates(); NUnit.Framework.Assert.IsNotNull(matches); NUnit.Framework.Assert.AreEqual(objects.Count, matches.Count); foreach (PackedObjectInfo info_1 in objects) { NUnit.Framework.Assert.IsTrue(matches.Contains(info_1), "contains " + info_1.Name ); } } NUnit.Framework.Assert.AreEqual(id, db.Resolve(id.Abbreviate(20).Name)); }