|
1 | 1 | #include <gtest/gtest.h> |
2 | 2 | #include <string> |
| 3 | +#include <algorithm> |
3 | 4 |
|
4 | 5 | template<typename T> |
5 | | -size_t adl_size(const T &s) |
6 | | -{ |
7 | | - return size(s); |
8 | | -} |
| 6 | +auto length(T t) { return 0; } |
9 | 7 |
|
10 | | - |
11 | | -namespace video { |
12 | | - struct Color { |
13 | | - int red, green, blue; |
14 | | - }; |
15 | | -} |
16 | 8 | namespace linear_algebra { |
17 | 9 |
|
18 | | - using Rows = size_t; |
19 | | - using Cols = size_t; |
20 | | - |
21 | | - template<Rows r, Cols c> |
22 | | - class Matrix { |
| 10 | + template<size_t N> |
| 11 | + class Vector { |
23 | 12 | public: |
24 | | - static auto constexpr size = r * c; |
25 | | - int values[size]; |
| 13 | + int values[N]; |
| 14 | + int norm() const { |
| 15 | + // TODO: |
| 16 | + // define norm so that 'longer' vectors |
| 17 | + // have bigger norms |
| 18 | + // HINT: std::reduce, std::accumulate |
| 19 | + return 0; |
| 20 | + }; |
| 21 | + |
26 | 22 | }; |
| 23 | + |
| 24 | + // for the EXPECT_EQ |
| 25 | + template<size_t N> |
| 26 | + bool operator==(const Vector<N> &v1, const Vector<N> &v2) { |
| 27 | + using namespace std; |
| 28 | + const auto mismatch = std::mismatch( |
| 29 | + begin(v1.values), end(v1.values), |
| 30 | + begin(v2.values) |
| 31 | + ); |
| 32 | + |
| 33 | + return mismatch.first == end(v1.values); |
| 34 | + } |
27 | 35 | } |
28 | 36 |
|
29 | | -TEST(name_lookup, DISABLED_we_can_overload_a_function_with_adl) |
| 37 | +template<typename T> |
| 38 | +auto sorted_by_increasing_length(const std::vector<T> &s) |
30 | 39 | { |
31 | | -//#define i_know_where_to_define_a_size_function |
32 | | - // TODO: enable previous line and add `size` overloads |
33 | | - // to make it compile |
34 | | - // GOAL: show that ADL uses the _whole_ namespace of the |
| 40 | + // TODO: adapt this function body to |
| 41 | + // return a sorted vector, where the order is |
| 42 | + // defined by a length function. |
| 43 | + // |
| 44 | + // GOAL: |
| 45 | + // define a generic function without |
| 46 | + // imposing changes on the types. |
| 47 | + // |
| 48 | + // HINT: use `std::sort` |
| 49 | + // |
| 50 | + return s; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +TEST(name_lookup, DISABLED_we_can_use_adl_to_adapt_to_a_library_function) |
| 55 | +{ |
| 56 | + // TODO: |
| 57 | + // define the sorted_by_increasing_length so that |
| 58 | + // the size is found by ADL (not by injection!) |
| 59 | + // |
| 60 | + // GOAL: show that ADL uses the namespace of the |
35 | 61 | // actual arguments |
36 | | - // HINT: start with the Color overload. The Matrix overload |
37 | | - // requires a template specialization: |
38 | | - // template<Rows r, Cols c> size_t size(const Matrix<r, c>&); |
39 | | - // LEVEL: INTERMEDIATE |
40 | | -//#define i_know_where_to_define_a_size_function |
41 | | -#ifdef i_know_where_to_define_a_size_function |
42 | | - EXPECT_EQ(5u, adl_size(std::string("abcde"))); |
43 | | - EXPECT_EQ(3u, adl_size(video::Color{0xff, 0x00, 0xff})); |
44 | | - EXPECT_EQ(12u, adl_size(linear_algebra::Matrix<3, 4>{})); |
45 | | -#endif |
| 62 | + { |
| 63 | + std::vector<std::string> strings{"xyzabcdx", "abc", "abcde", "abcdefg"}; |
| 64 | + auto sorted = sorted_by_increasing_length(strings); |
| 65 | + EXPECT_EQ("abc", sorted.at(0)); |
| 66 | + EXPECT_EQ("abcde", sorted.at(1)); |
| 67 | + EXPECT_EQ("abcdefg", sorted.at(2)); |
| 68 | + EXPECT_EQ("xyzabcdx", sorted.at(3)); |
| 69 | + } |
| 70 | + |
| 71 | + { |
| 72 | + using Point = linear_algebra::Vector<3>; |
| 73 | + std::vector<Point> points{ |
| 74 | + {0, 10, 0}, |
| 75 | + {10, 10, 0}, |
| 76 | + {1, 1, 1} |
| 77 | + }; |
| 78 | + auto sorted = sorted_by_increasing_length(points); |
| 79 | + EXPECT_EQ((Point{1, 1, 1}), sorted.at(0)); |
| 80 | + EXPECT_EQ((Point{0, 10, 0}), sorted.at(1)); |
| 81 | + EXPECT_EQ((Point{10, 10, 0}), sorted.at(2)); |
| 82 | + } |
46 | 83 | } |
0 commit comments