public void BuyContiguousTickets_SequencialTest() { // Initialize our box office var boxOffice = new TicketBoxOffice(4, 8); // Purchase some random seats Assert.IsNotNull(boxOffice.BuyTicketAtSeat(0, 3)); Assert.IsNotNull(boxOffice.BuyTicketAtSeat(1, 5)); Assert.IsNotNull(boxOffice.BuyTicketAtSeat(2, 2)); Assert.IsNotNull(boxOffice.BuyTicketAtSeat(2, 6)); //Assert.IsNotNull(boxOffice.BuyTicketAtSeat(3, 7)); // Verify they're sold Assert.IsNull(boxOffice.BuyTicketAtSeat(2, 6)); var tickets = boxOffice.BuyContiguousTickets(1); Assert.IsNotNull(tickets); Assert.AreEqual(1, tickets.Count); tickets = boxOffice.BuyContiguousTickets(3); Assert.IsNotNull(tickets); Assert.AreEqual(3, tickets.Count); tickets = boxOffice.BuyContiguousTickets(8); Assert.IsNotNull(tickets); Assert.AreEqual(8, tickets.Count); }
private void BuyContiguousTickets_ConcurrentTest_impl() { // Initialize our box office var boxOffice = new TicketBoxOffice(4, 8); // Purchase some random seats Parallel.Invoke( () => Assert.IsNotNull(boxOffice.BuyTicketAtSeat(0, 3)), () => Assert.IsNotNull(boxOffice.BuyTicketAtSeat(1, 5)), () => Assert.IsNotNull(boxOffice.BuyTicketAtSeat(2, 2)), () => Assert.IsNotNull(boxOffice.BuyTicketAtSeat(2, 6)) ); // Verify they're sold Assert.IsNull(boxOffice.BuyTicketAtSeat(2, 6)); Parallel.Invoke( () => { var tickets = boxOffice.BuyContiguousTickets(1); Assert.IsNotNull(tickets); Assert.AreEqual(1, tickets.Count); }, () => { var tickets = boxOffice.BuyContiguousTickets(3); Assert.IsNotNull(tickets); Assert.AreEqual(3, tickets.Count); }, () => { var tickets = boxOffice.BuyContiguousTickets(8); Assert.IsNotNull(tickets); Assert.AreEqual(8, tickets.Count); } ); }
private void BuyTicketAtSeat_ConcurrentTest_impl() { // Initialize our box office var boxOffice = new TicketBoxOffice(4, 8); Ticket t0 = null, t1 = null, t2 = null, t3 = null, t4 = null; // Purchase some random seats Parallel.Invoke( () => t0 = boxOffice.BuyTicketAtSeat(0, 3), () => t1 = boxOffice.BuyTicketAtSeat(1, 5), () => t2 = boxOffice.BuyTicketAtSeat(2, 2), () => t3 = boxOffice.BuyTicketAtSeat(2, 6), // This will try to buy a ticket that may have already been sold. () => t4 = boxOffice.BuyTicketAtSeat(2, 2) ); // Verify they're sold Assert.IsNotNull(t0); Assert.IsNotNull(t1); Assert.IsNotNull(t3); // Since buying ticket at duplicate seats only one of t2/t4 should've been successful Assert.IsTrue(t2 != null || t4 != null, "At least one of t2/t4 should've been successful."); Assert.IsTrue(t2 == null || t4 == null, "At least one of t2/t4 should've been unsuccessful."); }