Back to Repositories

Validating Integer Queue Operations in williamfiset/Algorithms

This test suite validates the implementation of an integer-based queue data structure, focusing on core queue operations and boundary conditions. The tests ensure proper functionality of queue operations like offer, poll, peek, and size management while maintaining data integrity.

Test Coverage Overview

The test suite provides comprehensive coverage of IntQueue operations:
  • Basic queue operations (isEmpty, size, offer, poll, peek)
  • Single element operations validation
  • Sequential operations with varying queue sizes
  • Comparison testing against Java’s ArrayDeque implementation
  • Edge cases including empty queue handling and capacity limits

Implementation Analysis

The testing approach employs JUnit Jupiter framework with Truth assertions for precise verification. The implementation uses systematic test patterns including:
  • Isolated unit tests for individual operations
  • Comprehensive integration tests for operation sequences
  • Random operation testing for robustness verification
  • Comparative testing against standard Java collections

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Google Truth for enhanced assertions
  • BeforeEach setup methods for test initialization
  • Java’s ArrayDeque for comparative testing
  • Random number generation for operation randomization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Systematic test organization with clear method naming
  • Comprehensive state validation after operations
  • Isolation of test cases for clear failure identification
  • Thorough validation of edge cases and normal operations
  • Randomized testing for robust verification

williamfiset/algorithms

src/test/java/com/williamfiset/algorithms/datastructures/queue/IntQueueTest.java

            
package com.williamfiset.algorithms.datastructures.queue;

import static com.google.common.truth.Truth.assertThat;

import java.util.*;
import org.junit.jupiter.api.*;

public class IntQueueTest {

  @BeforeEach
  public void setup() {}

  @Test
  public void testEmptyQueue() {
    IntQueue queue = new IntQueue(0);
    assertThat(queue.isEmpty()).isTrue();
    assertThat(queue.size()).isEqualTo(0);
  }

  // Doesn't apply to this implementation because of wrap
  // @Test(expected=Exception.class)
  // public void testPollOnEmpty() {
  //   IntQueue queue = new IntQueue(0);
  //   queue.poll();
  // }

  // Doesn't apply to this implementation because of wrap
  // @Test(expected=Exception.class)
  // public void testPeekOnEmpty() {
  //   IntQueue queue = new IntQueue(0);
  //   queue.peek();
  // }

  @Test
  public void testofferOneElement() {
    IntQueue queue = new IntQueue(1);
    queue.offer(77);
    assertThat(queue.size()).isEqualTo(1);
  }

  @Test
  public void testAll() {
    int n = 5;
    IntQueue queue = new IntQueue(10);
    assertThat(queue.isEmpty()).isTrue();
    for (int i = 1; i <= n; i++) {
      queue.offer(i);
      assertThat(queue.isEmpty()).isFalse();
    }
    for (int i = 1; i <= n; i++) {
      assertThat((int) queue.peek()).isEqualTo(i);
      assertThat((int) queue.poll()).isEqualTo(i);
      assertThat(queue.size()).isEqualTo(n - i);
    }
    assertThat(queue.isEmpty()).isTrue();
    n = 8;
    for (int i = 1; i <= n; i++) {
      queue.offer(i);
      assertThat(queue.isEmpty()).isFalse();
    }
    for (int i = 1; i <= n; i++) {
      assertThat((int) queue.peek()).isEqualTo(i);
      assertThat((int) queue.poll()).isEqualTo(i);
      assertThat(queue.size()).isEqualTo(n - i);
    }
    assertThat(queue.isEmpty()).isTrue();
    n = 9;
    for (int i = 1; i <= n; i++) {
      queue.offer(i);
      assertThat(queue.isEmpty()).isFalse();
    }
    for (int i = 1; i <= n; i++) {
      assertThat((int) queue.peek()).isEqualTo(i);
      assertThat((int) queue.poll()).isEqualTo(i);
      assertThat(queue.size()).isEqualTo(n - i);
    }
    assertThat(queue.isEmpty()).isTrue();
    n = 10;
    for (int i = 1; i <= n; i++) {
      queue.offer(i);
      assertThat(queue.isEmpty()).isFalse();
    }
    for (int i = 1; i <= n; i++) {
      assertThat((int) queue.peek()).isEqualTo(i);
      assertThat((int) queue.poll()).isEqualTo(i);
      assertThat(queue.size()).isEqualTo(n - i);
    }
    assertThat(queue.isEmpty()).isTrue();
  }

  @Test
  public void testPeekOneElement() {
    IntQueue queue = new IntQueue(1);
    queue.offer(77);
    assertThat(queue.peek()).isEqualTo(77);
    assertThat(queue.size()).isEqualTo(1);
  }

  @Test
  public void testpollOneElement() {
    IntQueue queue = new IntQueue(1);
    queue.offer(77);
    assertThat(queue.poll()).isEqualTo(77);
    assertThat(queue.size()).isEqualTo(0);
  }

  @Test
  public void testRandom() {

    for (int qSize = 1; qSize <= 50; qSize++) {

      IntQueue intQ = new IntQueue(qSize);
      ArrayDeque<Integer> javaQ = new ArrayDeque<>(qSize);

      assertThat(javaQ.isEmpty()).isEqualTo(intQ.isEmpty());
      assertThat(javaQ.size()).isEqualTo(intQ.size());

      for (int operations = 0; operations < 5000; operations++) {

        double r = Math.random();

        if (r < 0.60) {
          int elem = (int) (1000 * Math.random());
          if (javaQ.size() < qSize) {
            javaQ.offer(elem);
            intQ.offer(elem);
          }
        } else {
          if (!javaQ.isEmpty()) {
            assertThat((int) javaQ.poll()).isEqualTo((int) intQ.poll());
          }
        }

        assertThat(javaQ.isEmpty()).isEqualTo(intQ.isEmpty());
        assertThat(javaQ.size()).isEqualTo(intQ.size());
      }
    }
  }
}