\hypertarget{containers_8cpp_source}{}\doxysection{containers.\+cpp} \label{containers_8cpp_source}\index{tests/containers.cpp@{tests/containers.cpp}} \mbox{\hyperlink{containers_8cpp}{Go to the documentation of this file.}} \begin{DoxyCode}{0} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00001}00001 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{containers_8h}{kblib/containers.h}}"{}}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00002}00002 \textcolor{preprocessor}{\#include "{}catch.hpp"{}}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00003}00003 \textcolor{preprocessor}{\#include "{}\mbox{\hyperlink{algorithm_8h}{kblib/algorithm.h}}"{}}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00004}00004 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00005}00005 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00006}00006 \textcolor{preprocessor}{\#include }} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00007}00007 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00008}\mbox{\hyperlink{containers_8cpp_aeff71170dbc1dd55577739f753cc2ab3}{00008}} \mbox{\hyperlink{containers_8cpp_aeff71170dbc1dd55577739f753cc2ab3}{TEST\_CASE}}(\textcolor{stringliteral}{"{}try\_get"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00009}00009 std::map m;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00010}00010 \textcolor{keyword}{const} \textcolor{keyword}{auto}\& cm = m;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00011}00011 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00012}00012 \textcolor{comment}{// m[0] doesn't exist}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00013}00013 REQUIRE(\mbox{\hyperlink{namespacekblib_ac2b7bd4d50cea42484b9824a8140602f}{kblib::try\_get}}(m, 0) == \textcolor{keyword}{nullptr});} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00014}00014 \textcolor{comment}{// Works for const maps}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00015}00015 REQUIRE(\mbox{\hyperlink{namespacekblib_ac2b7bd4d50cea42484b9824a8140602f}{kblib::try\_get}}(cm, 0) == \textcolor{keyword}{nullptr});} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00016}00016 \textcolor{comment}{// Make m[0]}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00017}00017 m[0] = 0;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00018}00018 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00019}00019 \textcolor{comment}{// Basic correctness testing}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00020}00020 REQUIRE(\mbox{\hyperlink{namespacekblib_ac2b7bd4d50cea42484b9824a8140602f}{kblib::try\_get}}(m, 0) == \&m[0]);} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00021}00021 *\mbox{\hyperlink{namespacekblib_ac2b7bd4d50cea42484b9824a8140602f}{kblib::try\_get}}(m, 0) = 1;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00022}00022 REQUIRE(m[0] == 1);} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00023}00023 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00024}00024 \textcolor{comment}{// Returns pointer to const when given const map}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00025}00025 \textcolor{comment}{//*kblib::try\_get(cm, 0) = 1;}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00026}00026 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00027}00027 \textcolor{comment}{// Can't call for temporaries because it would return a dangling pointer}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00028}00028 \textcolor{comment}{// kblib::try\_get(std::map\{\{0, 1\}\}, 0);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00029}00029 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00030}00030 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00031}\mbox{\hyperlink{containers_8cpp_aa10bd92d3c2453912233efd21524edcb}{00031}} \mbox{\hyperlink{containers_8cpp_aeff71170dbc1dd55577739f753cc2ab3}{TEST\_CASE}}(\textcolor{stringliteral}{"{}build\_iterator"{}}, \textcolor{stringliteral}{"{}[build]"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00032}00032 \textcolor{keyword}{using }arr = std::array;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00033}00033 \textcolor{keyword}{const} arr input = \{2, 3, 5, 7, 11, 13, 17, 19\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00034}00034 \textcolor{keyword}{const} arr input2 = \{1, 1, 1, 1, 1, 1, 1, 1\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00035}00035 \textcolor{keyword}{const} \textcolor{keyword}{auto} unary\_f = [](\textcolor{keywordtype}{int} x) \{ \textcolor{keywordflow}{return} x * x; \};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00036}00036 \textcolor{keyword}{const} \textcolor{keyword}{auto} binary\_f = [](\textcolor{keywordtype}{int} \mbox{\hyperlink{namespacekblib_a22831dce77d5f35c13930b5e30a59f07}{a}}, \textcolor{keywordtype}{int} b) \{ \textcolor{keywordflow}{return} \mbox{\hyperlink{namespacekblib_a22831dce77d5f35c13930b5e30a59f07}{a}} -\/ b; \};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00037}00037 \textcolor{keyword}{const} arr squared = \{4, 9, 25, 49, 121, 169, 289, 361\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00038}00038 \textcolor{keyword}{const} arr pminusone = \{1, 2, 4, 6, 10, 12, 16, 18\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00039}00039 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00040}00040 \textcolor{keyword}{const} \textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}} = [](\textcolor{keyword}{auto} \mbox{\hyperlink{namespacekblib_a22831dce77d5f35c13930b5e30a59f07}{a}}, \textcolor{keyword}{auto} b) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00041}00041 \textcolor{keywordflow}{return} \mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{std::equal}}(std::begin(\mbox{\hyperlink{namespacekblib_a22831dce77d5f35c13930b5e30a59f07}{a}}), std::end(\mbox{\hyperlink{namespacekblib_a22831dce77d5f35c13930b5e30a59f07}{a}}), std::begin(b), std::end(b));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00042}00042 \};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00043}00043 [[gnu::unused]] \textcolor{keyword}{auto} print\_arr = [\&](\textcolor{keyword}{auto} c) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00044}00044 \textcolor{keywordflow}{for} (\textcolor{keyword}{const} \textcolor{keyword}{auto}\& v : c) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00045}00045 std::cout << v << \textcolor{stringliteral}{"{}, "{}};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00046}00046 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00047}00047 std::cout << \textcolor{charliteral}{'\(\backslash\)n'};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00048}00048 \};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00049}00049 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00050}00050 SECTION(\textcolor{stringliteral}{"{}unary dynamic build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00051}00051 \textcolor{comment}{// auto built = kblib::build>(input.begin(), input.end(),}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00052}00052 \textcolor{comment}{// unary\_f);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00053}00053 \textcolor{keyword}{auto} built} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00054}00054 = \mbox{\hyperlink{namespacekblib_aff53cc55f02129e6192d7a87c7cdf7f0}{std::transform}}(input.begin(), input.end(),} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00055}00055 \mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}>\{\}, unary\_f)} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00056}00056 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00057}00057 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(squared, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00058}00058 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00059}00059 SECTION(\textcolor{stringliteral}{"{}binary dynamic build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00060}00060 \textcolor{comment}{// auto built = kblib::build>(input.begin(), input.end(),}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00061}00061 \textcolor{comment}{// input2.begin(), binary\_f);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00062}00062 \textcolor{keyword}{auto} built} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00063}00063 = \mbox{\hyperlink{namespacekblib_aff53cc55f02129e6192d7a87c7cdf7f0}{std::transform}}(input.begin(), input.end(), input2.begin(),} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00064}00064 \mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}>\{\}, binary\_f)} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00065}00065 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00066}00066 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(pminusone, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00067}00067 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00068}00068 SECTION(\textcolor{stringliteral}{"{}unary array build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00069}00069 \textcolor{comment}{// auto built = kblib::build(input.begin(), input.end(), unary\_f);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00070}00070 \textcolor{keyword}{auto} built = \mbox{\hyperlink{namespacekblib_aff53cc55f02129e6192d7a87c7cdf7f0}{std::transform}}(input.begin(), input.end(),} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00071}00071 \mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}\{\}, unary\_f)} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00072}00072 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00073}00073 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(squared, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00074}00074 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00075}00075 SECTION(\textcolor{stringliteral}{"{}binary array build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00076}00076 \textcolor{comment}{// auto built = kblib::build(input.begin(), input.end(),}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00077}00077 \textcolor{comment}{// input2.begin(), binary\_f);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00078}00078 \textcolor{keyword}{auto} built = \mbox{\hyperlink{namespacekblib_aff53cc55f02129e6192d7a87c7cdf7f0}{std::transform}}(input.begin(), input.end(), input2.begin(),} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00079}00079 \mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}\{\}, binary\_f)} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00080}00080 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00081}00081 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(pminusone, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00082}00082 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00083}00083 \textcolor{keyword}{const} arr \mbox{\hyperlink{namespacekblib_a40d0e463984f5ab01f6db106dbad9fb0}{iota}} = \{0, 1, 2, 3, 4, 5, 6, 7\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00084}00084 SECTION(\textcolor{stringliteral}{"{}dynamic generator build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00085}00085 \textcolor{comment}{// auto built = kblib::build>([x = 0]() mutable \{ return}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00086}00086 \textcolor{comment}{// x++; \}, 8);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00087}00087 \textcolor{keyword}{auto} built = \mbox{\hyperlink{namespacekblib_a44de39f5ab34ae1bf5b037eb11155caa}{std::generate\_n}}(\mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}>\{\}, 8,} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00088}00088 [x = 0]() \textcolor{keyword}{mutable} \{ \textcolor{keywordflow}{return} x++; \})} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00089}00089 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00090}00090 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(\mbox{\hyperlink{namespacekblib_a40d0e463984f5ab01f6db106dbad9fb0}{iota}}, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00091}00091 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00092}00092 SECTION(\textcolor{stringliteral}{"{}array generator build"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00093}00093 \textcolor{comment}{// auto built = kblib::build([x = 0]() mutable \{ return x++; \});}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00094}00094 \textcolor{keyword}{auto} built} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00095}00095 = \mbox{\hyperlink{namespacekblib_a33f20e37682017226d1d7a7128915ea5}{kblib::generate}}(\mbox{\hyperlink{classkblib_1_1build__iterator}{kblib::build\_iterator}}\{\}, \mbox{\hyperlink{namespacekblib_aad99c3a2af651595f17e2c95c2c521d4}{kblib::build\_end}},} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00096}00096 [x = 0]() \textcolor{keyword}{mutable} \{ \textcolor{keywordflow}{return} x++; \})} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00097}00097 .base();} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00098}00098 \textcolor{comment}{// Using only the standard library, it would have to be this in order to}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00099}00099 \textcolor{comment}{// avoid a magic number for the size.}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00100}00100 \textcolor{comment}{// auto built = std::generate\_n(kblib::build\_iterator\{\},}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00101}00101 \textcolor{comment}{// std::tuple\_size::value,}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00102}00102 \textcolor{comment}{// [x = 0]() mutable \{ return x++; \})}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00103}00103 \textcolor{comment}{// .base();}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00104}00104 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(\mbox{\hyperlink{namespacekblib_a40d0e463984f5ab01f6db106dbad9fb0}{iota}}, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00105}00105 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00106}00106 \textcolor{comment}{// std::iota returns void, not the iterator}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00107}00107 SECTION(\textcolor{stringliteral}{"{}dynamic buildiota"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00108}00108 \textcolor{comment}{// auto built = kblib::buildiota>(8, 0);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00109}00109 \textcolor{comment}{// auto built = ...}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00110}00110 \textcolor{keyword}{auto} built} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00111}00111 = kblib::construct\_from\_range>(\mbox{\hyperlink{namespacekblib_a72690688922a2d4ae9a3e46ff0c1a9a9}{kblib::range}}(0, 8));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00112}00112 REQUIRE(\mbox{\hyperlink{namespacekblib_afbec3807c1be9d0aea88c620d673823c}{equal}}(\mbox{\hyperlink{namespacekblib_a40d0e463984f5ab01f6db106dbad9fb0}{iota}}, built));} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00113}00113 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00114}00114 SECTION(\textcolor{stringliteral}{"{}array buildiota"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00115}00115 \textcolor{comment}{// auto built = kblib::buildiota(0);}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00116}00116 \textcolor{comment}{// auto built = ...}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00117}00117 \textcolor{comment}{// REQUIRE(equal(iota, built));}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00118}00118 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00119}00119 \}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00120}00120 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00121}\mbox{\hyperlink{containers_8cpp_a35266a2a239b747a1a5c74df47602b52}{00121}} \mbox{\hyperlink{containers_8cpp_aeff71170dbc1dd55577739f753cc2ab3}{TEST\_CASE}}(\textcolor{stringliteral}{"{}build\_iterator is nodiscard"{}}) \{} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00122}00122 \textcolor{keyword}{using }arr = std::array;} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00123}00123 \mbox{\hyperlink{tdecl_8h_a607cffc585fe3bfcb06b63be2948de4a}{KBLIB\_UNUSED}} \textcolor{keyword}{const} arr input = \{2, 3, 5, 7, 11, 13, 17, 19\};} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00124}00124 } \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00125}00125 \textcolor{comment}{// This line correctly generates a warning that the return value is ignored.}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00126}00126 \textcolor{comment}{// std::copy(input.begin(), input.end(), kblib::build\_iterator\{\});}} \DoxyCodeLine{\Hypertarget{containers_8cpp_source_l00127}00127 \}} \end{DoxyCode}