You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
238 lines
11 KiB
238 lines
11 KiB
/* Copyright 2010 10gen Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
|
|
using MongoDB.Bson;
|
|
|
|
namespace MongoDB.BsonUnitTests {
|
|
[TestFixture]
|
|
public class BsonObjectIdTests {
|
|
[Test]
|
|
public void TestByteArrayConstructor() {
|
|
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
|
|
var objectId = new BsonObjectId(bytes);
|
|
Assert.AreEqual(0x01020304, objectId.Timestamp);
|
|
Assert.AreEqual(0x050607, objectId.Machine);
|
|
Assert.AreEqual(0x0809, objectId.Pid);
|
|
Assert.AreEqual(0x0a0b0c, objectId.Increment);
|
|
Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement);
|
|
Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
|
|
Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
|
|
Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
|
|
}
|
|
|
|
[Test]
|
|
public void TestIntLongConstructor() {
|
|
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
|
|
var objectId = new BsonObjectId(0x01020304, 0x05060708090a0b0cL);
|
|
Assert.AreEqual(0x01020304, objectId.Timestamp);
|
|
Assert.AreEqual(0x050607, objectId.Machine);
|
|
Assert.AreEqual(0x0809, objectId.Pid);
|
|
Assert.AreEqual(0x0a0b0c, objectId.Increment);
|
|
Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement);
|
|
Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
|
|
Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
|
|
Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
|
|
}
|
|
|
|
[Test]
|
|
public void TestStringConstructor() {
|
|
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
|
|
var objectId = new BsonObjectId("0102030405060708090a0b0c");
|
|
Assert.AreEqual(0x01020304, objectId.Timestamp);
|
|
Assert.AreEqual(0x050607, objectId.Machine);
|
|
Assert.AreEqual(0x0809, objectId.Pid);
|
|
Assert.AreEqual(0x0a0b0c, objectId.Increment);
|
|
Assert.AreEqual(0x05060708090a0b0c, objectId.MachinePidIncrement);
|
|
Assert.AreEqual(BsonConstants.UnixEpoch.AddSeconds(0x01020304), objectId.CreationTime);
|
|
Assert.AreEqual("0102030405060708090a0b0c", objectId.ToString());
|
|
Assert.IsTrue(bytes.SequenceEqual(objectId.ToByteArray()));
|
|
}
|
|
|
|
[Test]
|
|
public void TestIComparable() {
|
|
var objectId1 = BsonObjectId.GenerateNewId();
|
|
var objectId2 = BsonObjectId.GenerateNewId();
|
|
Assert.AreEqual(0, objectId1.CompareTo(objectId1));
|
|
Assert.AreEqual(-1, objectId1.CompareTo(objectId2));
|
|
Assert.AreEqual(1, objectId2.CompareTo(objectId1));
|
|
Assert.AreEqual(0, objectId2.CompareTo(objectId2));
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareEqualGeneratedIds() {
|
|
var objectId1 = BsonObjectId.GenerateNewId();
|
|
var objectId2 = objectId1;
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsFalse(objectId1 != objectId2);
|
|
Assert.IsTrue(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareSmallerTimestamp() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030505060708090a0b0c");
|
|
Assert.IsTrue(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsFalse(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareSmallerMachine() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030405060808090a0b0c");
|
|
Assert.IsTrue(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsFalse(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareSmallerPid() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0c");
|
|
var objectId2 = new BsonObjectId("01020304050607080a0a0b0c");
|
|
Assert.IsTrue(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsFalse(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareSmallerIncrement() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030405060708090a0b0d");
|
|
Assert.IsTrue(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsFalse(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareSmallerGeneratedId() {
|
|
var objectId1 = BsonObjectId.GenerateNewId();
|
|
var objectId2 = BsonObjectId.GenerateNewId();
|
|
Assert.IsTrue(objectId1 < objectId2);
|
|
Assert.IsTrue(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsFalse(objectId1 > objectId2);
|
|
Assert.IsFalse(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareLargerTimestamp() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030305060708090a0b0c");
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsFalse(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsTrue(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareLargerMachine() {
|
|
var objectId1 = new BsonObjectId("0102030405060808090a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030405060708090a0b0c");
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsFalse(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsTrue(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareLargerPid() {
|
|
var objectId1 = new BsonObjectId("01020304050607080a0a0b0c");
|
|
var objectId2 = new BsonObjectId("0102030405060708090a0b0c");
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsFalse(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsTrue(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareLargerIncrement() {
|
|
var objectId1 = new BsonObjectId("0102030405060708090a0b0d");
|
|
var objectId2 = new BsonObjectId("0102030405060708090a0b0c");
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsFalse(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsTrue(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCompareLargerGeneratedId() {
|
|
var objectId2 = BsonObjectId.GenerateNewId(); // generate before objectId2
|
|
var objectId1 = BsonObjectId.GenerateNewId();
|
|
Assert.IsFalse(objectId1 < objectId2);
|
|
Assert.IsFalse(objectId1 <= objectId2);
|
|
Assert.IsTrue(objectId1 != objectId2);
|
|
Assert.IsFalse(objectId1 == objectId2);
|
|
Assert.IsTrue(objectId1 > objectId2);
|
|
Assert.IsTrue(objectId1 >= objectId2);
|
|
}
|
|
|
|
[Test]
|
|
public void TestParse() {
|
|
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
|
|
var objectId1 = BsonObjectId.Parse("0102030405060708090a0b0c"); // lower case
|
|
var objectId2 = BsonObjectId.Parse("0102030405060708090A0B0C"); // upper case
|
|
Assert.IsTrue(objectId1.ToByteArray().SequenceEqual(objectId2.ToByteArray()));
|
|
Assert.IsTrue(objectId1.ToString() == "0102030405060708090a0b0c"); // ToString returns lower case
|
|
Assert.IsTrue(objectId1.ToString() == objectId2.ToString());
|
|
Assert.Throws<FormatException>(() => BsonObjectId.Parse("102030405060708090a0b0c")); // too short
|
|
Assert.Throws<FormatException>(() => BsonObjectId.Parse("x102030405060708090a0b0c")); // invalid character
|
|
Assert.Throws<FormatException>(() => BsonObjectId.Parse("00102030405060708090a0b0c")); // too long
|
|
}
|
|
|
|
[Test]
|
|
public void TestTryParse() {
|
|
byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
|
|
BsonObjectId objectId1, objectId2;
|
|
Assert.IsTrue(BsonObjectId.TryParse("0102030405060708090a0b0c", out objectId1)); // lower case
|
|
Assert.IsTrue(BsonObjectId.TryParse("0102030405060708090A0B0C", out objectId2)); // upper case
|
|
Assert.IsTrue(objectId1.ToByteArray().SequenceEqual(objectId2.ToByteArray()));
|
|
Assert.IsTrue(objectId1.ToString() == "0102030405060708090a0b0c"); // ToString returns lower case
|
|
Assert.IsTrue(objectId1.ToString() == objectId2.ToString());
|
|
Assert.IsFalse(BsonObjectId.TryParse("102030405060708090a0b0c", out objectId1)); // too short
|
|
Assert.IsFalse(BsonObjectId.TryParse("x102030405060708090a0b0c", out objectId1)); // invalid character
|
|
Assert.IsFalse(BsonObjectId.TryParse("00102030405060708090a0b0c", out objectId1)); // too long
|
|
}
|
|
}
|
|
}
|