Exemplo n.º 1
0
/**
 * item() and operator[] tests
 */
void testItem() {
  IntVector numbers;
  numbers.push(5);
  numbers.push(4);
  numbers.push(3);
  numbers.push(2);
  numbers.push(1);

  IntVector numbersCopy = numbers;
  assert(numbersCopy.item(1) == 4);
  numbersCopy.item(1) = 40;
  assert(numbersCopy.item(1) == 40);
  
  try {
    numbersCopy.item(5);
    fprintf(stderr, "Did not generate OutOfBoundsException on invalid item() access.\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }

  numbersCopy = numbers;
  assert(numbersCopy[1] == 4);
  numbersCopy[1] = 40;
  assert(numbersCopy[1] == 40);

  try {
    numbersCopy[5];
    fprintf(stderr, "Did not generate OutOfBoundsException on invalid [] access.\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }

  numbersCopy = numbers;
  const IntVector& numbersRef = numbersCopy;
  assert(numbersRef.item(1) == 4);
  
  try {
    numbersRef.item(5);
    fprintf(stderr, "Did not generate OutOfBoundsException on invalid item() const access.\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }

  assert(numbersRef[1] == 4);

  try {
    numbersRef[5];
    fprintf(stderr, "Did not generate OutOfBoundsException on invalid [] const access.\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }
}
Exemplo n.º 2
0
/**
 * clear() tests
 */
void testClear() {
  IntVector v;
  v.push(0);
  v.push(1);
  v.push(2);
  
  v.clear();
  
  assert(v.size() == 0);
  
  // test empty
  v.clear();
  assert(v.size() == 0);
}
Exemplo n.º 3
0
/**
 * operator=() tests
 */
void testAssignment() {
  IntVector orig;
  orig.push(1);
  orig.push(2);
  orig.push(3);
  
  IntVector copy;
  copy = orig;
  
  assert(copy.size() == 3);
  assert(copy[0] == 1);
  assert(copy[1] == 2);
  assert(copy[2] == 3);
}
Exemplo n.º 4
0
/**
 * shift() tests
 */
void testShift() {
  IntVector v;
  v.push(1);
  v.push(2);
  v.push(3);
  
  assert(v.shift() == 1);
  assert(v.shift() == 2);
  assert(v.shift() == 3);
  
  try {
    v.shift();
    fprintf(stderr, "Allowed shift on empty list.\n");
  } catch (OutOfBoundsException&) {
    // expected
  }
}
Exemplo n.º 5
0
/**
 * operator!= tests
 */
void testInequality() {
  IntVector orig;
  orig.push(1);
  orig.push(2);
  orig.push(3);
  
  IntVector copy;
  copy.push(1);
  copy.push(2);
  copy.push(3);
  
  assert(! (orig != copy));
  
  IntVector tooShort;
  tooShort.push(1);
  tooShort.push(2);
  
  assert(tooShort != orig);
  
  IntVector differentValues;
  differentValues.push(1);
  differentValues.push(2);
  differentValues.push(4);
  
  assert(differentValues != orig);
}
Exemplo n.º 6
0
/**
 * operator== tests
 */
void testEquality() {
  IntVector orig;
  orig.push(1);
  orig.push(2);
  orig.push(3);
  
  IntVector copy;
  copy.push(1);
  copy.push(2);
  copy.push(3);
  
  assert(orig == copy);
  
  IntVector tooShort;
  tooShort.push(1);
  tooShort.push(2);
  
  assert(! (tooShort == orig));
  
  IntVector differentValues;
  differentValues.push(1);
  differentValues.push(2);
  differentValues.push(4);
  
  assert(! (differentValues == orig));
}
Exemplo n.º 7
0
/**
 * append() tests
 */
void testAppend() {
  IntVector v;
  v.push(0);
  v.append(5);
  
  assert(v.size() == 2);
  assert(v[0] == 0);
  assert(v[1] == 5);
}
Exemplo n.º 8
0
/**
 * unshift() tests
 */
void testUnshift() {
  IntVector v;
  v.push(0);
  v.unshift(5);
  
  assert(v.size() == 2);
  assert(v[0] == 5);
  assert(v[1] == 0);
}
Exemplo n.º 9
0
/**
 * push() tests
 */
void testPush() {
  IntVector v;
  v.insert(0, 0);
  v.push(5);
  
  assert(v.size() == 2);
  assert(v[0] == 0);
  assert(v[1] == 5);
}
Exemplo n.º 10
0
/**
 * remove() tests
 */
void testRemove() {
  IntVector v;
  v.push(1);
  v.push(2);
  v.push(3);
  v.push(4);
  v.push(5);

  // middle
  int i = v.remove(2);
  assert(i == 3);
  assert(v.size() == 4);
  assert(v[0] == 1);
  assert(v[1] == 2);
  assert(v[2] == 4);
  assert(v[3] == 5);
  
  // beginning
  i = v.remove(0);
  assert(i == 1);
  assert(v.size() == 3);
  assert(v[0] == 2);
  assert(v[1] == 4);
  assert(v[2] == 5);
  
  // end
  i = v.remove(2);
  assert(i == 5);
  assert(v.size() == 2);
  assert(v[0] == 2);
  assert(v[1] == 4);
  
  // bad index
  try {
    i = v.remove(10);
    fprintf(stderr, "Allowed removal of non-existent index.\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }
}
Exemplo n.º 11
0
/**
 * insert() tests
 */
void testInsert() {
  // insert middle
  IntVector v;
  v.push(2);
  v.push(6);
  v.insert(1, 4);
  
  assert(v.size() == 3);
  assert(v[0] == 2);
  assert(v[1] == 4);
  assert(v[2] == 6);

  // insert beginning
  v.insert(0, 0);
  assert(v.size() == 4);
  assert(v[0] == 0);
  assert(v[1] == 2);
  assert(v[2] == 4);
  assert(v[3] == 6);
  
  // insert end
  v.insert(4, 8);
  assert(v.size() == 5);
  assert(v[0] == 0);
  assert(v[1] == 2);
  assert(v[2] == 4);
  assert(v[3] == 6);
  assert(v[4] == 8);
  
  // invalid index
  try {
    v.insert(10, 100);
    fprintf(stderr, "Allowed insertion at invalid index\n");
    exit(1);
  } catch (OutOfBoundsException&) {
    // expected
  }
}