Chapter 13 Exercise Set 0: Chapter Review

  1. Create a directory named Cards and download src.tgz into this directory. Run:

    tar xzvf src.tgz
    

    in the unix command shell to extract the directory containing the following source files:

    • Cards.h

    • Cards.cpp

    • random.h

    • random.cpp

    • test_random.cpp

    • test_cards.cpp

    • test_decks.cpp

    You can remove the .tgz file after extracting it.

  2. Add a file named Makefile in the Cards directory (along side the src subdirectory) with the following contents:

    CC=g++
    STD=c++11
    
    build/%.o: src/%.cpp
    	@mkdir -p build
    	@$(CC) -MM -MT $@ $< > build/$*.d
    	$(CC) -c -o $@ $< -std=$(STD)
    
    build/test_random: build/test_random.o build/random.o 
    	$(CC) -o $@ $^ -std=$(STD)
    
    build/test_cards: build/test_cards.o build/random.o build/Cards.o
    	$(CC) -o $@ $^ -std=$(STD)
    
    build/test_decks: build/test_decks.o build/random.o build/Cards.o 
    	$(CC) -o $@ $^ -std=$(STD) 
    
    -include build/*.d
    
    .PHONY: test all clean
    
    test_random: build/test_random
    	./build/test_random
    
    test_cards: build/test_cards
    	./build/test_cards
    
    test_decks: build/test_decks
    	./build/test_decks
    
    test: test_random test_cards test_decks
    
    clean:
    	rm -rf build
    
  3. Assuming you have Make installed on your system, you should now be able to run the following commands:

    - make test_random
    - make test_cards
    - make test_decks
    - make test
    - make clean
    

    Try each of these to see what they do.

  4. Take a look at the tests in test_random.cpp. How do they “test” that the function behaves as desired? Can you think of any possible problems with the approach taken?