Ejemplo n.º 1
0
 public void Types()
 {
     Assert.IsTrue(typeof(IRoom).IsInterface);
     var r = new Room(35.0, RoomStandard.Apartment);
     Assert.IsTrue(typeof(Room).GetInterfaces().Contains(typeof(IRoom)));
     Assert.AreEqual(35.0, (r as IRoom).SquareMeters);
     Assert.IsTrue(typeof(IHotel).IsInterface);
     Assert.IsTrue(typeof(Hotel<Room, SquareMetersComparer>).GetInterfaces().Contains(typeof(IHotel)));
     var h = new Hotel<Room, SquareMetersComparer>(5, r);
     Assert.AreEqual((uint)5, (h as IHotel).Stars);
     Assert.IsTrue(typeof(SquareMetersComparer).GetInterfaces().Contains(typeof(IComparer<Room>)));
     Assert.IsTrue(typeof(StandardMultipliedSquareMetersComparer).GetInterfaces().Contains(typeof(IComparer<Room>)));
     Assert.AreEqual(typeof(SquareMetersComparer).BaseType, typeof(StandardMultipliedSquareMetersComparer).BaseType);
     Assert.AreNotEqual(typeof(SquareMetersComparer).BaseType, typeof(object));
     Assert.AreNotEqual(typeof(SquareMetersComparer).BaseType, typeof(ValueType));
     Assert.IsTrue(typeof(SquareMetersComparer).BaseType.IsAbstract);
     Assert.AreNotEqual(typeof(SquareMetersComparer).BaseType, typeof(Comparer<Room>));
 }
 public override int Compare(Room x, Room y)
 {
     if (x != null && y != null)
     {
         return (x.SquareMeters*(byte) x.Apartment).CompareTo(y.SquareMeters*(byte) y.Apartment);
     }
     else
     {
         if(x == null && y ==null)
         {
             return 0;
         }
         else if (x == null)
         {
             return -1;
         }
         else return 1;
     }
     
 }
        public override int Compare(Room x, Room y)
        {            
            if (x != null && y != null)
            {
                return x.SquareMeters.CompareTo(y.SquareMeters);
            }
            else
            {
                if (x == null && y == null)
                {
                    return 0;
                }
                else if (x == null)
                {
                    return -1;
                }
                else return 1;
            }

        }
Ejemplo n.º 4
0
 public void Constructor()
 {
     var r1 = new Room(35.0, RoomStandard.Apartment);
     var r2 = new Room(24.5, RoomStandard.Student);
     var h1 = new Hotel<Room, SquareMetersComparer>(3, r1);
     Assert.AreEqual((uint)3, h1.Stars);
     var h2 = new Hotel<Room, SquareMetersComparer>(3, r1, r2);
     var h3 = new Hotel<Room, SquareMetersComparer>(3, new Room[] { r1, r2 });
     var ht = typeof(Hotel<Room, SquareMetersComparer>);
     var constructors = ht.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance);
     var parameterlessConstructor = constructors.FirstOrDefault(c => c.GetParameters().Count() == 0);
     var noEmptyConstructorMessage = String.Format("Parameterless constructor {0} of type {1} is redundant.", parameterlessConstructor, ht.Name);
     Assert.IsNull(parameterlessConstructor, noEmptyConstructorMessage);
     var arrayConstructor = constructors.Where(c => c.GetParameters().Count() > 0)
         .FirstOrDefault(c => c.GetParameters().Last().ParameterType == typeof(Room[]));
     var arrayConstructorCustomAttribues = arrayConstructor.GetParameters().Last().CustomAttributes;
     var incorrectSignatureMessage = String.Format("Missing variable argument list parameter modifier \"params\" assisting array of type {0} in constructor {1} of type {2}.", r1.GetType().Name, arrayConstructor, ht.Name);
     Assert.IsTrue(arrayConstructorCustomAttribues.Count(a => a.AttributeType == typeof(ParamArrayAttribute)) == 1, incorrectSignatureMessage);
     var redundantConstructorsMessage = String.Format("Additional constructors accepting {0} instances are redundant in type {1}.", r1.GetType().Name, ht.Name);
     var spareConstructorsCount = constructors.Where(c => c.GetParameters().Count() > 0)
         .Count(c => c.GetParameters().Last().ParameterType == typeof(Room));
     Assert.AreEqual(0, spareConstructorsCount, redundantConstructorsMessage);
 }
Ejemplo n.º 5
0
 public void Comparers()
 {
     var smsmc = new StandardMultipliedSquareMetersComparer();
     var smc = new SquareMetersComparer();
     var r1 = new Room(35.0, RoomStandard.Apartment);
     Assert.AreEqual(0, smsmc.Compare(new Room(105.0, RoomStandard.Student), r1)); // 35*3x=105x
     var r2 = new Room(25.0, RoomStandard.Standard);
     Assert.AreEqual(0, smsmc.Compare(new Room(50.0, RoomStandard.Student), r2)); // 25*2x=50x
     var r3 = new Room(40.0, RoomStandard.Student); // 40*1x=40x
     var rooms = new Room[] { r1, r2, r3 };
     var h1 = new Hotel<Room, SquareMetersComparer>(5, rooms);
     Assert.IsTrue(h1.AsEnumerable().SequenceEqual(new Room[] { r2, r1, r3 }));
     var h2 = new Hotel<Room, StandardMultipliedSquareMetersComparer>(5, rooms);
      Assert.IsTrue(h2.AsEnumerable().SequenceEqual(new Room[] { r3, r2, r1 }));
     Assert.AreEqual(-1, smsmc.Compare(null, r1));
     Assert.AreEqual(-1, smc.Compare(null, r1));
     Assert.AreEqual(1, smsmc.Compare(r1, null));
     Assert.AreEqual(1, smc.Compare(r1, null));
     Assert.AreEqual(0, smsmc.Compare(null, null));
     Assert.AreEqual(0, smc.Compare(null, null));
     var r4 = new Room(25.0, RoomStandard.Standard);
     Assert.AreEqual(0, smsmc.Compare(r2, r4));
     Assert.AreEqual(0, smc.Compare(r2, r4));
 }
Ejemplo n.º 6
0
        public void Booking()
        {
            var r1 = new Room(35.0, RoomStandard.Apartment);
            var r2 = new Room(25.0, RoomStandard.Standard);
            var r3 = new Room(40.0, RoomStandard.Student);
            var rooms = new Room[] { r1, r2, r3 };
            var h1 = new Hotel<Room, SquareMetersComparer>(5, rooms);
            var h2 = new Hotel<Room, SquareMetersComparer>(5, rooms);
            var now = DateTime.Now;
            Assert.IsTrue(h1.IsFree(r2, now.AddDays(10), now.AddDays(30)));
            h1.Book(r2, now.AddDays(10), now.AddDays(30));
            h2.Book(r2, now.AddDays(10), now.AddDays(30));
            Assert.IsFalse(h1.IsFree(r2, now.AddDays(10), now.AddDays(30)));
            Assert.IsTrue(h1.IsFree(r1, now.AddDays(10), now.AddDays(30)));
            Assert.IsTrue(h1.IsFree(r3, now.AddDays(10), now.AddDays(30)));
            Assert.IsFalse(h1.IsFree(r2, now.AddDays(1), now.AddDays(20)));
            Assert.IsFalse(h1.IsFree(r2, now.AddDays(20), now.AddDays(40)));
            Assert.IsFalse(h1.IsFree(r2, now.AddDays(9), now.AddDays(31)));
            Assert.IsFalse(h1.IsFree(r2, now.AddDays(11), now.AddDays(29)));
            Assert.IsTrue(h1.IsFree(r2, now.AddDays(1), now.AddDays(9)));
            Assert.IsTrue(h1.IsFree(r2, now.AddDays(31), now.AddDays(40)));
            Assert.IsTrue(h1.IsFree(r2, now.AddHours(1), now.AddHours(2)));
            h1.Book(r2, now.AddHours(1), now.AddHours(2));
            Assert.IsTrue(h2.IsFree(r2, now.AddHours(1), now.AddHours(2)));
            Assert.IsTrue(h1.IsFree(r2, now.AddHours(3), now.AddHours(4)));
            h1.Book(r2, now.AddHours(3), now.AddHours(4));
            var r4 = new Room(25.0, RoomStandard.Standard);
            var forbidden = new Action[]
            {
                () => h1.IsFree(r2, now.AddDays(2), now.AddDays(1)),
                () => h1.Book(r2, now.AddDays(2), now.AddDays(1)),
                () => h1.IsFree(r4, now.AddDays(1), now.AddDays(2)),
                () => h1.Book(r4, now.AddDays(1), now.AddDays(2)),
                () => h1.Book(r2, now, now.AddDays(2)),
                () => h1.Book(r2, now.AddDays(10), now.AddDays(30))
            };

            foreach (var action in forbidden)
            {
                try
                {
                    action();
                    Assert.Fail("Invalid operation performed.");
                }
                catch (ArgumentOutOfRangeException)
                {
                }
                catch (AssertFailedException)
                {
                    throw;
                }
                catch
                {
                    Assert.Fail("Invalid exception type.");
                }
            }
        }