|
| 1 | +# Exercises for the masses |
| 2 | + |
| 3 | +## History |
| 4 | + |
| 5 | +Working in C++ around the year 2010 was very exciting. The language was about |
| 6 | +to receive some vast new impulses, promising better semantics, performance, … |
| 7 | + |
| 8 | +In 2016, I was lucky enough to work in an environment that lived on the edge: |
| 9 | +constantly following the latest (Microsoft) compiler versions, and writing C++ |
| 10 | +programs according to the latest and greatest idioms. |
| 11 | + |
| 12 | +All of this happiness had to be shared. That’s why my employer, Sioux Embedded |
| 13 | +Systems, asked if anyone would be able to create a C++11/14 ramp-up course for |
| 14 | +my colleagues. Of course I bit. |
| 15 | + |
| 16 | +The budget: 5 days of preparation - a challenge for me. It seemed a vast |
| 17 | +undertaking, given the fact that I hadn’t been giving training, nothing ‘free’ |
| 18 | +could be found. So I decided I would borrow free and open source presentations, |
| 19 | +and create exercises myself. But how? |
| 20 | + |
| 21 | +As it happened, there also was a gap in the knowledge of unit testing |
| 22 | +frameworks. So why not kill two birds with one stone? Fond of TDD, I had grown |
| 23 | +quite familiar with the googletest framework. So I decided to create a bunch of |
| 24 | +failing unit tests that had to be ‘fixed’ using C++11/14 features. |
| 25 | + |
| 26 | +Since I was to take all my course material from the internet, I thought it only |
| 27 | +fitting to give back. So I started creating the course in the open on |
| 28 | +https://github.com/xtofl/cpp11training/. |
| 29 | + |
| 30 | +## Overview |
| 31 | + |
| 32 | +### Repo structure |
| 33 | + |
| 34 | +* Presentations are stored in the `slides/` directory. There's also a |
| 35 | + [readme](slides/README.md) to explain its contents. |
| 36 | +* Exercises are stored per topic under the `src/` folder; within each topic, |
| 37 | + exercise source files are numbered. |
| 38 | +* Each topic file contains a number of `TEST` cases. Each of these cases represents |
| 39 | + an exercise. In the comments, I try to explain what you're supposed to do (TODO), |
| 40 | + what you're supposed to learn from this exercise (GOAL), and may add some HINTs. |
| 41 | + |
| 42 | +### Exercise Example |
| 43 | + |
| 44 | +For instance, one of the first exercises you'll encounter is about using the |
| 45 | +range-based-for feature introduced in C++11. |
| 46 | + |
| 47 | +```c++ |
| 48 | +TEST(range_based_for, DISABLED_we_can_iterate_over_a_collection) |
| 49 | +{ |
| 50 | + int result = 0; |
| 51 | + // TODO: extend the Range class so that it acts as a |
| 52 | + // real 'range' and can be used in a range based for loop. |
| 53 | + // |
| 54 | + // GOAL: understand the working of range-based for loop, and |
| 55 | + // adapt existing classes to it |
| 56 | + // |
| 57 | + // HINT: This is a harder problem: you need to provide |
| 58 | + // a free begin(const Range&) and end(const Range&) function |
| 59 | + // that returns an iterator-like object. |
| 60 | + // |
| 61 | +#ifdef solv |
| 62 | + for (const auto &element : Range{ 1, 11 }) |
| 63 | + { |
| 64 | + result += element; |
| 65 | + } |
| 66 | +#endif |
| 67 | + EXPECT_EQ(55, result); |
| 68 | +} |
| 69 | +``` |
| 70 | +
|
| 71 | +As you'll see, the test will |
| 72 | +be compiled, but the `DISABLED_` prefix in its name will prevent gtest from |
| 73 | +executing it. Removing the prefix 'activates' the test and makes it run, |
| 74 | +but fail: |
| 75 | +
|
| 76 | + |
| 77 | +
|
| 78 | +Another example: one to learn using the `transform` algorithm: |
| 79 | +
|
| 80 | +```c++ |
| 81 | +TEST(apply_transform, DISABLED_join_two_input_ranges) |
| 82 | +{ |
| 83 | + const std::vector<std::string> keys{ {"two", "five", "ten", "forty"} }; |
| 84 | + const std::vector<int> values{ {2, 5, 10, 40} }; |
| 85 | +
|
| 86 | + std::vector<std::string> numbers; |
| 87 | + // TODO: transform keys and values into |
| 88 | + // this python-key-value syntax |
| 89 | + // GOAL: see that `transform` can accept two ranges |
| 90 | + // and 'zip' them together |
| 91 | + ASSERT_EQ(keys.size(), numbers.size()); |
| 92 | + EXPECT_EQ("two: 2", numbers[0]); |
| 93 | + EXPECT_EQ("five: 5", numbers[1]); |
| 94 | + EXPECT_EQ("ten: 10", numbers[2]); |
| 95 | + EXPECT_EQ("forty: 40", numbers[3]); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Future |
| 100 | + |
| 101 | +Meanwhile we're 2020, and I have given this training about 5 times. Everytime, |
| 102 | +the overall response is positive about offering running code in the form of |
| 103 | +isolated unit tests. The fun part for me is that whenever someone finds a |
| 104 | +mistake, I can kindly ask them to fix it and create a merge request. |
| 105 | + |
| 106 | +And it has proven to be needed. What is also needed: some of the exercises are |
| 107 | +a little 'far fetched', and may be replaced by more down to earth subjects. |
| 108 | +Although on every occasion I am able to improve the guidelines, add some |
| 109 | +exercises and the lot, this body of exercises still needs a lot of guidance. |
| 110 | + |
| 111 | +I'm hoping that some day, this effort will be picked up and improved by 'the |
| 112 | +masses'. For I'm convinced that good and affordable programming education is |
| 113 | +an important ingredient for an inclusive world. |
0 commit comments