示例#1
0
  static void takeCardTest(){
    ostringstream oss;
    Stock stock = Stock();
    vector<int> list(30,5);

    stock += list;
    int size = stock.getSize();
    
    for(int i = 0; i < 30; i++){
      assert(stock.takeCard() == 5);
      assert(stock.getSize() == --size);
    }

    assert(stock.isEmpty());
    
    try {
      stock.takeCard();
    }
    catch(std::logic_error & e){
      oss << e.what();
    }

    cout << oss.str();
    
  }
示例#2
0
  static void opsTest() {
    Stock s = Stock();
    vector<int> list;
    ostringstream oss;

    for(int i = 1; i < 13; i++){
      list.push_back(i);
    }

    int num = 1;
    try {
      s += list; num++;
      s += list; num++;
      s += list; num++; //this num++ should not be executed
    }
    catch (std::invalid_argument & e){
      oss << "Failed at num = " << num;
    }
    assert(oss.str() == "Failed at num = 3");
    oss.str("");
    oss.clear();

    //assert Stock pile has 24 cards now
    assert(s.getSize() == 24);

    try {
      s += 13;
    }
    catch (std::invalid_argument & e){
      oss << e.what();
    }

    //assert exception is caught
    assert(oss.str() == "Invalid card value.\n");
    oss.str("");
    oss.clear();

    vector<int> list2 = {5, 3, 2, 1, 0, 1};

    try{
      s += list2;
    } catch(std::invalid_argument & e){
      assert(false); //should not be executed
    }

    assert(s.getSize() == 30);

    try {
      s += 1;
    }catch(std::logic_error & e){
      oss << e.what();
    }

    assert(oss.str() == "Can't have more than 30 cards in Stock pile!\n");
  }
示例#3
0
  static void moveTest(){
    ostringstream oss;
    Stock stock = Stock();
    Draw draw = Draw();

    int size;
    try {
      draw.move(stock, size = 26);
      assert(stock.getSize() == 26);
      
      draw.move(stock, size = 5);
    } catch(std::logic_error & e){
      oss << "Failure while trying to take " << size << " cards!";
    }
    assert(oss.str() == "Failure while trying to take 5 cards!");    
    oss.str("");
    oss.clear();
  
    Build build = Build();
    stock += 3;
    try {
      stock.move(build);
      assert(false);
    }
    catch(std::exception & e){
      oss << e.what();
    }
    assert(oss.str() == "Can't add card - not in sequence!\n");    
  }  
示例#4
0
 static void constructorTest() {
   Stock s = Stock();
   assert(s.getSize() == 0);
   assert(s.isEmpty());
 }