### Question:
BoundedPriorityQueue { public Result offer( E element ) { int updatedCount = count.incrementAndGet(); if ( updatedCount > config.maxCount ) { count.decrementAndGet(); return E_COUNT_EXCEEDED; } long elementBytes = sizeOf.apply( element ); long updatedBytes = bytes.addAndGet( elementBytes ); if ( elementBytes != 0 && updatedCount > config.minCount ) { if ( updatedBytes > config.maxBytes ) { bytes.addAndGet( -elementBytes ); count.decrementAndGet(); return E_SIZE_EXCEEDED; } } if ( !queue.offer( new StableElement( element ) ) ) { throw new IllegalStateException(); } return OK; } BoundedPriorityQueue( Config config, Function<E,Long> sizeOf, java.util.Comparator<E> comparator ); int count(); long bytes(); Result offer( E element ); Optional<E> poll(); Optional<E> poll( int timeout, TimeUnit unit );  }### Answer:
@Test public void shouldNotAllowMoreThanMaxBytes() { BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<>( BASE_CONFIG, Integer::longValue, NO_PRIORITY ); assertEquals( E_SIZE_EXCEEDED, queue.offer( 101 ) ); assertEquals( OK, queue.offer( 99 ) ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( E_SIZE_EXCEEDED, queue.offer( 1 ) ); }
@Test public void shouldAllowMinCountDespiteSizeLimit() { Config config = new Config( 2, 5, 100 ); BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<>( config, Integer::longValue, NO_PRIORITY ); assertEquals( OK, queue.offer( 101 ) ); assertEquals( OK, queue.offer( 101 ) ); assertEquals( E_SIZE_EXCEEDED, queue.offer( 1 ) ); }
@Test public void shouldAllowZeroSizedItemsDespiteSizeLimit() { BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<>( BASE_CONFIG, Integer::longValue, NO_PRIORITY ); assertEquals( OK, queue.offer( 100 ) ); assertEquals( E_SIZE_EXCEEDED, queue.offer( 1 ) ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( OK, queue.offer( 0 ) ); }
@Test public void shouldNotAllowMoreThanMaxCount() { BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<>( BASE_CONFIG, Integer::longValue, NO_PRIORITY ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( OK, queue.offer( 1 ) ); assertEquals( E_COUNT_EXCEEDED, queue.offer( 1 ) ); }
@Test public void shouldNotAllowMoreThanMaxCountDespiteZeroSize() { BoundedPriorityQueue<Integer> queue = new BoundedPriorityQueue<>( BASE_CONFIG, Integer::longValue, NO_PRIORITY ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( OK, queue.offer( 0 ) ); assertEquals( E_COUNT_EXCEEDED, queue.offer( 0 ) ); }