[{"data":1,"prerenderedAt":2033},["ShallowReactive",2],{"doc-generators":3},{"id":4,"title":5,"body":6,"description":120,"extension":2025,"meta":2026,"navigation":1536,"path":2027,"seo":2028,"slug":2029,"source":2030,"stem":2031,"__hash__":2032},"docs\u002Fdocs\u002Fgenerators.md","Data Generators",{"type":7,"value":8,"toc":2012},"minimark",[9,50,54,93,98,111,114,197,220,286,296,304,321,478,484,499,581,584,724,742,745,748,878,892,899,906,909,1044,1059,1067,1074,1082,1085,1243,1261,1279,1288,1299,1389,1394,1402,1417,1430,1435,1438,1445,1455,1458,1464,1769,1777,1784,1800,1808,1812,1815,1818,1841,1844,1848,1851,1857,1909,1916,1929,1996,2008],[10,11,12],"blockquote",{},[13,14,15,19,20,24,25,28,29,32,33,36,37,36,40,43,44,49],"p",{},[16,17,18],"strong",{},"Catch3 compatibility reference:"," This page describes the test-writing API Catch3 shares with Catch2: the macros, such as ",[21,22,23],"code",{},"TEST_CASE"," and ",[21,26,27],{},"REQUIRE",", and the ",[21,30,31],{},"Catch::"," namespace keep Catch2's names. Headers and build names are Catch3's own (",[21,34,35],{},"\u003CProgmasoft\u002FCatch3\u002FTestMacros.hpp>",", ",[21,38,39],{},"find_package(Catch3)",[21,41,42],{},"Catch3::Catch3WithMain","). See ",[45,46,48],"a",{"href":47},"\u002Fdocs\u002Fprogmasoft-catch3\u002F","Catch3 additions"," for Progmasoft APIs.",[51,52,5],"h1",{"id":53},"data-generators",[13,55,56,59,62,73,75,79,81,85,87,91],{},[16,57,58],{},"Contents",[60,61],"br",{},[45,63,65,66,24,69,72],{"href":64},"#combining-generate-and-section","Combining ",[21,67,68],{},"GENERATE",[21,70,71],{},"SECTION",".",[60,74],{},[45,76,78],{"href":77},"#provided-generators","Provided generators",[60,80],{},[45,82,84],{"href":83},"#generator-interface","Generator interface",[60,86],{},[45,88,90],{"href":89},"#other-usage-examples","Other usage examples",[60,92],{},[10,94,95],{},[13,96,97],{},"Introduced in Catch2 2.6.0.",[13,99,100,101,105,106,24,108,110],{},"Data generators (also known as ",[102,103,104],"em",{},"data driven\u002Fparametrized test cases",")\nlet you reuse the same set of assertions across different input values.\nIn Catch2, this means that they respect the ordering and nesting\nof the ",[21,107,23],{},[21,109,71],{}," macros, and their nested sections\nare run once per each value in a generator.",[13,112,113],{},"This is best explained with an example:",[115,116,121],"pre",{"className":117,"code":118,"language":119,"meta":120,"style":120},"language-cpp shiki shiki-themes github-dark","TEST_CASE(\"Generators\") {\n    auto i = GENERATE(1, 3, 5);\n    REQUIRE(is_odd(i));\n}\n","cpp","",[21,122,123,142,177,191],{"__ignoreMap":120},[124,125,128,131,135,139],"span",{"class":126,"line":127},"line",1,[124,129,23],{"class":130},"svObZ",[124,132,134],{"class":133},"s95oV","(",[124,136,138],{"class":137},"sU2Wk","\"Generators\"",[124,140,141],{"class":133},") {\n",[124,143,145,149,152,155,158,160,164,166,169,171,174],{"class":126,"line":144},2,[124,146,148],{"class":147},"snl16","    auto",[124,150,151],{"class":133}," i ",[124,153,154],{"class":147},"=",[124,156,157],{"class":130}," GENERATE",[124,159,134],{"class":133},[124,161,163],{"class":162},"sDLfK","1",[124,165,36],{"class":133},[124,167,168],{"class":162},"3",[124,170,36],{"class":133},[124,172,173],{"class":162},"5",[124,175,176],{"class":133},");\n",[124,178,180,183,185,188],{"class":126,"line":179},3,[124,181,182],{"class":130},"    REQUIRE",[124,184,134],{"class":133},[124,186,187],{"class":130},"is_odd",[124,189,190],{"class":133},"(i));\n",[124,192,194],{"class":126,"line":193},4,[124,195,196],{"class":133},"}\n",[13,198,199,200,202,203,206,207,209,210,212,213,216,217,72],{},"The \"Generators\" ",[21,201,23],{}," will be entered 3 times, and the value of\n",[21,204,205],{},"i"," will be 1, 3, and 5 in turn. ",[21,208,68],{},"s can also be used multiple\ntimes at the same scope, in which case the result will be a cartesian\nproduct of all elements in the generators. This means that in the snippet\nbelow, the test case will be run 6 (2*3) times. The ",[21,211,68],{}," macro\nis defined in the ",[21,214,215],{},"Progmasoft\u002FCatch3\u002FGenerators\u002FGenerators.hpp"," header, so compiling\nthe code examples below also requires\n",[21,218,219],{},"#include \u003CProgmasoft\u002FCatch3\u002FGenerators\u002FGenerators.hpp>",[115,221,223],{"className":117,"code":222,"language":119,"meta":120,"style":120},"TEST_CASE(\"Generators\") {\n    auto i = GENERATE(1, 2);\n    auto j = GENERATE(3, 4, 5);\n}\n",[21,224,225,235,256,282],{"__ignoreMap":120},[124,226,227,229,231,233],{"class":126,"line":127},[124,228,23],{"class":130},[124,230,134],{"class":133},[124,232,138],{"class":137},[124,234,141],{"class":133},[124,236,237,239,241,243,245,247,249,251,254],{"class":126,"line":144},[124,238,148],{"class":147},[124,240,151],{"class":133},[124,242,154],{"class":147},[124,244,157],{"class":130},[124,246,134],{"class":133},[124,248,163],{"class":162},[124,250,36],{"class":133},[124,252,253],{"class":162},"2",[124,255,176],{"class":133},[124,257,258,260,263,265,267,269,271,273,276,278,280],{"class":126,"line":179},[124,259,148],{"class":147},[124,261,262],{"class":133}," j ",[124,264,154],{"class":147},[124,266,157],{"class":130},[124,268,134],{"class":133},[124,270,168],{"class":162},[124,272,36],{"class":133},[124,274,275],{"class":162},"4",[124,277,36],{"class":133},[124,279,173],{"class":162},[124,281,176],{"class":133},[124,283,284],{"class":126,"line":193},[124,285,196],{"class":133},[13,287,288,289,291,292,295],{},"There are 2 parts to generators in Catch2, the ",[21,290,68],{}," macro together\nwith the already provided generators, and the ",[21,293,294],{},"IGenerator\u003CT>"," interface\nthat allows users to implement their own generators.",[297,298,65,300,24,302,72],"h2",{"id":299},"combining-generate-and-section",[21,301,68],{},[21,303,71],{},[13,305,306,308,309,311,312,314,315,317,318,320],{},[21,307,68],{}," can be seen as an implicit ",[21,310,71],{},", that goes from the place\n",[21,313,68],{}," is used, to the end of the scope. This can be used for various\neffects. The simplest usage is shown below, where the ",[21,316,71],{}," \"one\"\nruns 4 (2*2) times, and ",[21,319,71],{}," \"two\" is run 6 times (2*3).",[115,322,324],{"className":117,"code":323,"language":119,"meta":120,"style":120},"TEST_CASE(\"Generators\") {\n    auto i = GENERATE(1, 2);\n    SECTION(\"one\") {\n        auto j = GENERATE(-3, -2);\n        REQUIRE(j \u003C i);\n    }\n    SECTION(\"two\") {\n        auto k = GENERATE(4, 5, 6);\n        REQUIRE(i != k);\n    }\n}\n",[21,325,326,336,356,368,394,409,415,427,454,468,473],{"__ignoreMap":120},[124,327,328,330,332,334],{"class":126,"line":127},[124,329,23],{"class":130},[124,331,134],{"class":133},[124,333,138],{"class":137},[124,335,141],{"class":133},[124,337,338,340,342,344,346,348,350,352,354],{"class":126,"line":144},[124,339,148],{"class":147},[124,341,151],{"class":133},[124,343,154],{"class":147},[124,345,157],{"class":130},[124,347,134],{"class":133},[124,349,163],{"class":162},[124,351,36],{"class":133},[124,353,253],{"class":162},[124,355,176],{"class":133},[124,357,358,361,363,366],{"class":126,"line":179},[124,359,360],{"class":130},"    SECTION",[124,362,134],{"class":133},[124,364,365],{"class":137},"\"one\"",[124,367,141],{"class":133},[124,369,370,373,375,377,379,381,384,386,388,390,392],{"class":126,"line":193},[124,371,372],{"class":147},"        auto",[124,374,262],{"class":133},[124,376,154],{"class":147},[124,378,157],{"class":130},[124,380,134],{"class":133},[124,382,383],{"class":147},"-",[124,385,168],{"class":162},[124,387,36],{"class":133},[124,389,383],{"class":147},[124,391,253],{"class":162},[124,393,176],{"class":133},[124,395,397,400,403,406],{"class":126,"line":396},5,[124,398,399],{"class":130},"        REQUIRE",[124,401,402],{"class":133},"(j ",[124,404,405],{"class":147},"\u003C",[124,407,408],{"class":133}," i);\n",[124,410,412],{"class":126,"line":411},6,[124,413,414],{"class":133},"    }\n",[124,416,418,420,422,425],{"class":126,"line":417},7,[124,419,360],{"class":130},[124,421,134],{"class":133},[124,423,424],{"class":137},"\"two\"",[124,426,141],{"class":133},[124,428,430,432,435,437,439,441,443,445,447,449,452],{"class":126,"line":429},8,[124,431,372],{"class":147},[124,433,434],{"class":133}," k ",[124,436,154],{"class":147},[124,438,157],{"class":130},[124,440,134],{"class":133},[124,442,275],{"class":162},[124,444,36],{"class":133},[124,446,173],{"class":162},[124,448,36],{"class":133},[124,450,451],{"class":162},"6",[124,453,176],{"class":133},[124,455,457,459,462,465],{"class":126,"line":456},9,[124,458,399],{"class":130},[124,460,461],{"class":133},"(i ",[124,463,464],{"class":147},"!=",[124,466,467],{"class":133}," k);\n",[124,469,471],{"class":126,"line":470},10,[124,472,414],{"class":133},[124,474,476],{"class":126,"line":475},11,[124,477,196],{"class":133},[13,479,480,481,483],{},"The specific order of the ",[21,482,71],{},"s will be \"one\", \"one\", \"two\", \"two\",\n\"two\", \"one\"...",[13,485,486,487,489,490,492,493,495,496,498],{},"The fact that ",[21,488,68],{}," introduces a virtual ",[21,491,71],{}," can also be used\nto make a generator replay only some ",[21,494,71],{},"s, without having to\nexplicitly add a ",[21,497,71],{},". As an example, the code below reports 3\nassertions, because the \"first\" section is run once, but the \"second\"\nsection is run twice.",[115,500,502],{"className":117,"code":501,"language":119,"meta":120,"style":120},"TEST_CASE(\"GENERATE between SECTIONs\") {\n    SECTION(\"first\") { REQUIRE(true); }\n    auto _ = GENERATE(1, 2);\n    SECTION(\"second\") { REQUIRE(true); }\n}\n",[21,503,504,515,537,558,577],{"__ignoreMap":120},[124,505,506,508,510,513],{"class":126,"line":127},[124,507,23],{"class":130},[124,509,134],{"class":133},[124,511,512],{"class":137},"\"GENERATE between SECTIONs\"",[124,514,141],{"class":133},[124,516,517,519,521,524,527,529,531,534],{"class":126,"line":144},[124,518,360],{"class":130},[124,520,134],{"class":133},[124,522,523],{"class":137},"\"first\"",[124,525,526],{"class":133},") { ",[124,528,27],{"class":130},[124,530,134],{"class":133},[124,532,533],{"class":162},"true",[124,535,536],{"class":133},"); }\n",[124,538,539,541,544,546,548,550,552,554,556],{"class":126,"line":179},[124,540,148],{"class":147},[124,542,543],{"class":133}," _ ",[124,545,154],{"class":147},[124,547,157],{"class":130},[124,549,134],{"class":133},[124,551,163],{"class":162},[124,553,36],{"class":133},[124,555,253],{"class":162},[124,557,176],{"class":133},[124,559,560,562,564,567,569,571,573,575],{"class":126,"line":193},[124,561,360],{"class":130},[124,563,134],{"class":133},[124,565,566],{"class":137},"\"second\"",[124,568,526],{"class":133},[124,570,27],{"class":130},[124,572,134],{"class":133},[124,574,533],{"class":162},[124,576,536],{"class":133},[124,578,579],{"class":126,"line":396},[124,580,196],{"class":133},[13,582,583],{},"This can lead to surprisingly complex test flows. As an example, the test\nbelow will report 14 assertions:",[115,585,587],{"className":117,"code":586,"language":119,"meta":120,"style":120},"TEST_CASE(\"Complex mix of sections and generates\") {\n    auto i = GENERATE(1, 2);\n    SECTION(\"A\") {\n        SUCCEED(\"A\");\n    }\n    auto j = GENERATE(3, 4);\n    SECTION(\"B\") {\n        SUCCEED(\"B\");\n    }\n    auto k = GENERATE(5, 6);\n    SUCCEED();\n}\n",[21,588,589,600,620,631,642,646,666,677,687,691,711,719],{"__ignoreMap":120},[124,590,591,593,595,598],{"class":126,"line":127},[124,592,23],{"class":130},[124,594,134],{"class":133},[124,596,597],{"class":137},"\"Complex mix of sections and generates\"",[124,599,141],{"class":133},[124,601,602,604,606,608,610,612,614,616,618],{"class":126,"line":144},[124,603,148],{"class":147},[124,605,151],{"class":133},[124,607,154],{"class":147},[124,609,157],{"class":130},[124,611,134],{"class":133},[124,613,163],{"class":162},[124,615,36],{"class":133},[124,617,253],{"class":162},[124,619,176],{"class":133},[124,621,622,624,626,629],{"class":126,"line":179},[124,623,360],{"class":130},[124,625,134],{"class":133},[124,627,628],{"class":137},"\"A\"",[124,630,141],{"class":133},[124,632,633,636,638,640],{"class":126,"line":193},[124,634,635],{"class":130},"        SUCCEED",[124,637,134],{"class":133},[124,639,628],{"class":137},[124,641,176],{"class":133},[124,643,644],{"class":126,"line":396},[124,645,414],{"class":133},[124,647,648,650,652,654,656,658,660,662,664],{"class":126,"line":411},[124,649,148],{"class":147},[124,651,262],{"class":133},[124,653,154],{"class":147},[124,655,157],{"class":130},[124,657,134],{"class":133},[124,659,168],{"class":162},[124,661,36],{"class":133},[124,663,275],{"class":162},[124,665,176],{"class":133},[124,667,668,670,672,675],{"class":126,"line":417},[124,669,360],{"class":130},[124,671,134],{"class":133},[124,673,674],{"class":137},"\"B\"",[124,676,141],{"class":133},[124,678,679,681,683,685],{"class":126,"line":429},[124,680,635],{"class":130},[124,682,134],{"class":133},[124,684,674],{"class":137},[124,686,176],{"class":133},[124,688,689],{"class":126,"line":456},[124,690,414],{"class":133},[124,692,693,695,697,699,701,703,705,707,709],{"class":126,"line":470},[124,694,148],{"class":147},[124,696,434],{"class":133},[124,698,154],{"class":147},[124,700,157],{"class":130},[124,702,134],{"class":133},[124,704,173],{"class":162},[124,706,36],{"class":133},[124,708,451],{"class":162},[124,710,176],{"class":133},[124,712,713,716],{"class":126,"line":475},[124,714,715],{"class":130},"    SUCCEED",[124,717,718],{"class":133},"();\n",[124,720,722],{"class":126,"line":721},12,[124,723,196],{"class":133},[10,725,726],{},[13,727,728,729,731,732,734,735,741],{},"The ability to place ",[21,730,68],{}," between two ",[21,733,71],{},"s was ",[45,736,740],{"href":737,"rel":738},"https:\u002F\u002Fgithub.com\u002Fcatchorg\u002FCatch2\u002Fissues\u002F1938",[739],"nofollow","introduced"," in Catch2 2.13.0.",[297,743,78],{"id":744},"provided-generators",[13,746,747],{},"Catch2's provided generator functionality consists of three parts,",[749,750,751,757,774,834,854],"ul",{},[752,753,754,756],"li",{},[21,755,68],{}," macro,  that serves to integrate generator expression with\na test case,",[752,758,759,760],{},"2 fundamental generators\n",[749,761,762,768],{},[752,763,764,767],{},[21,765,766],{},"SingleValueGenerator\u003CT>"," -- contains only single element",[752,769,770,773],{},[21,771,772],{},"FixedValuesGenerator\u003CT>"," -- contains multiple elements",[752,775,776,777,780,781],{},"6 generic generators that modify other generators (defined in ",[21,778,779],{},"Progmasoft\u002FCatch3\u002FGenerators\u002FAdapters.hpp",")\n",[749,782,783,789,799,808,818,828],{},[752,784,785,788],{},[21,786,787],{},"FilterGenerator\u003CT, Predicate>"," -- filters out elements from a generator\nfor which the predicate returns \"false\"",[752,790,791,794,795,798],{},[21,792,793],{},"TakeGenerator\u003CT>"," -- takes first ",[21,796,797],{},"n"," elements from a generator",[752,800,801,804,805,807],{},[21,802,803],{},"RepeatGenerator\u003CT>"," -- repeats output from a generator ",[21,806,797],{}," times",[752,809,810,813,814,817],{},[21,811,812],{},"MapGenerator\u003CT, U, Func>"," -- returns the result of applying ",[21,815,816],{},"Func","\non elements from a different generator",[752,819,820,823,824,827],{},[21,821,822],{},"ChunkGenerator\u003CT>"," -- returns chunks (inside ",[21,825,826],{},"std::vector",") of n elements from a generator",[752,829,830,833],{},[21,831,832],{},"ConcatGenerator\u003CT>"," -- returns elements from multiple generators as if they were one",[752,835,836,837,780,840],{},"2 random generators (defined in ",[21,838,839],{},"Progmasoft\u002FCatch3\u002FGenerators\u002FRandom.hpp",[749,841,842,848],{},[752,843,844,847],{},[21,845,846],{},"RandomIntegerGenerator\u003CIntegral>"," -- generates random Integrals from range",[752,849,850,853],{},[21,851,852],{},"RandomFloatGenerator\u003CFloat>"," -- generates random Floats from range",[752,855,856,857,780,860],{},"2 range generators (defined in ",[21,858,859],{},"Progmasoft\u002FCatch3\u002FGenerators\u002FRange.hpp",[749,861,862,872],{},[752,863,864,867,868,871],{},[21,865,866],{},"RangeGenerator\u003CT>(first, last)"," -- generates all values inside a ",[21,869,870],{},"[first, last)"," arithmetic range",[752,873,874,877],{},[21,875,876],{},"IteratorGenerator\u003CT>"," -- copies and returns values from an iterator range",[10,879,880],{},[13,881,882,36,884,36,886,24,888,891],{},[21,883,822],{},[21,885,846],{},[21,887,852],{},[21,889,890],{},"RangeGenerator\u003CT>"," were introduced in Catch2 2.7.0.",[10,893,894],{},[13,895,896,898],{},[21,897,876],{}," was introduced in Catch2 2.10.0.",[10,900,901],{},[13,902,903,905],{},[21,904,832],{}," was introduced in Catch2 3.13.0",[13,907,908],{},"The generators also have associated helper functions that infer their\ntype, making their usage much nicer. These are",[749,910,911,919,926,934,941,948,955,974,985,992,1004,1015,1023,1030,1037],{},[752,912,913,916,917],{},[21,914,915],{},"value(T&&)"," for ",[21,918,766],{},[752,920,921,916,924],{},[21,922,923],{},"values(std::initializer_list\u003CT>)",[21,925,772],{},[752,927,928,916,931],{},[21,929,930],{},"table\u003CTs...>(std::initializer_list\u003Cstd::tuple\u003CTs...>>)",[21,932,933],{},"FixedValuesGenerator\u003Cstd::tuple\u003CTs...>>",[752,935,936,916,939],{},[21,937,938],{},"filter(predicate, GeneratorWrapper\u003CT>&&)",[21,940,787],{},[752,942,943,916,946],{},[21,944,945],{},"take(count, GeneratorWrapper\u003CT>&&)",[21,947,793],{},[752,949,950,916,953],{},[21,951,952],{},"repeat(repeats, GeneratorWrapper\u003CT>&&)",[21,954,803],{},[752,956,957,916,960,962,963,966,967,970,971,973],{},[21,958,959],{},"map(func, GeneratorWrapper\u003CT>&&)",[21,961,812],{}," (map ",[21,964,965],{},"U"," to ",[21,968,969],{},"T",", deduced from ",[21,972,816],{},")",[752,975,976,916,979,962,981,966,983,973],{},[21,977,978],{},"map\u003CT>(func, GeneratorWrapper\u003CU>&&)",[21,980,812],{},[21,982,965],{},[21,984,969],{},[752,986,987,916,990],{},[21,988,989],{},"chunk(chunk-size, GeneratorWrapper\u003CT>&&)",[21,991,822],{},[752,993,994,916,997,1000,1001],{},[21,995,996],{},"random(IntegerOrFloat a, IntegerOrFloat b)",[21,998,999],{},"RandomIntegerGenerator"," or ",[21,1002,1003],{},"RandomFloatGenerator",[752,1005,1006,916,1009,1012,1013],{},[21,1007,1008],{},"range(Arithmetic start, Arithmetic end)",[21,1010,1011],{},"RangeGenerator\u003CArithmetic>"," with a step size of ",[21,1014,163],{},[752,1016,1017,916,1020,1022],{},[21,1018,1019],{},"range(Arithmetic start, Arithmetic end, Arithmetic step)",[21,1021,1011],{}," with a custom step size",[752,1024,1025,916,1028],{},[21,1026,1027],{},"from_range(InputIterator from, InputIterator to)",[21,1029,876],{},[752,1031,1032,916,1035],{},[21,1033,1034],{},"from_range(Container const&)",[21,1036,876],{},[752,1038,1039,916,1042],{},[21,1040,1041],{},"cat(GeneratorWrapper\u003CT>&&...)",[21,1043,832],{},[10,1045,1046],{},[13,1047,1048,36,1051,1054,1055,1058],{},[21,1049,1050],{},"chunk()",[21,1052,1053],{},"random()"," and both ",[21,1056,1057],{},"range()"," functions were introduced in Catch2 2.7.0.",[10,1060,1061],{},[13,1062,1063,1066],{},[21,1064,1065],{},"from_range"," has been introduced in Catch2 2.10.0",[10,1068,1069],{},[13,1070,1071,1073],{},[21,1072,1057],{}," for floating point numbers has been introduced in Catch2 2.11.0",[10,1075,1076],{},[13,1077,1078,1081],{},[21,1079,1080],{},"cat"," has been introduced in Catch2 3.13.0",[13,1083,1084],{},"And can be used as shown in the example below to create a generator\nthat returns 100 odd random number:",[115,1086,1088],{"className":117,"code":1087,"language":119,"meta":120,"style":120},"TEST_CASE(\"Generating random ints\", \"[example][generator]\") {\n    SECTION(\"Deducing functions\") {\n        auto i = GENERATE(take(100, filter([](int i) { return i % 2 == 1; }, random(-100, 100))));\n        REQUIRE(i > -100);\n        REQUIRE(i \u003C 100);\n        REQUIRE(i % 2 == 1);\n    }\n}\n",[21,1089,1090,1106,1117,1190,1206,1219,1235,1239],{"__ignoreMap":120},[124,1091,1092,1094,1096,1099,1101,1104],{"class":126,"line":127},[124,1093,23],{"class":130},[124,1095,134],{"class":133},[124,1097,1098],{"class":137},"\"Generating random ints\"",[124,1100,36],{"class":133},[124,1102,1103],{"class":137},"\"[example][generator]\"",[124,1105,141],{"class":133},[124,1107,1108,1110,1112,1115],{"class":126,"line":144},[124,1109,360],{"class":130},[124,1111,134],{"class":133},[124,1113,1114],{"class":137},"\"Deducing functions\"",[124,1116,141],{"class":133},[124,1118,1119,1121,1123,1125,1127,1129,1132,1134,1137,1139,1142,1145,1148,1152,1154,1157,1159,1162,1165,1168,1171,1174,1177,1179,1181,1183,1185,1187],{"class":126,"line":179},[124,1120,372],{"class":147},[124,1122,151],{"class":133},[124,1124,154],{"class":147},[124,1126,157],{"class":130},[124,1128,134],{"class":133},[124,1130,1131],{"class":130},"take",[124,1133,134],{"class":133},[124,1135,1136],{"class":162},"100",[124,1138,36],{"class":133},[124,1140,1141],{"class":130},"filter",[124,1143,1144],{"class":133},"([](",[124,1146,1147],{"class":147},"int",[124,1149,1151],{"class":1150},"s9osk"," i",[124,1153,526],{"class":133},[124,1155,1156],{"class":147},"return",[124,1158,151],{"class":133},[124,1160,1161],{"class":147},"%",[124,1163,1164],{"class":162}," 2",[124,1166,1167],{"class":147}," ==",[124,1169,1170],{"class":162}," 1",[124,1172,1173],{"class":133},"; }, ",[124,1175,1176],{"class":130},"random",[124,1178,134],{"class":133},[124,1180,383],{"class":147},[124,1182,1136],{"class":162},[124,1184,36],{"class":133},[124,1186,1136],{"class":162},[124,1188,1189],{"class":133},"))));\n",[124,1191,1192,1194,1196,1199,1202,1204],{"class":126,"line":193},[124,1193,399],{"class":130},[124,1195,461],{"class":133},[124,1197,1198],{"class":147},">",[124,1200,1201],{"class":147}," -",[124,1203,1136],{"class":162},[124,1205,176],{"class":133},[124,1207,1208,1210,1212,1214,1217],{"class":126,"line":396},[124,1209,399],{"class":130},[124,1211,461],{"class":133},[124,1213,405],{"class":147},[124,1215,1216],{"class":162}," 100",[124,1218,176],{"class":133},[124,1220,1221,1223,1225,1227,1229,1231,1233],{"class":126,"line":411},[124,1222,399],{"class":130},[124,1224,461],{"class":133},[124,1226,1161],{"class":147},[124,1228,1164],{"class":162},[124,1230,1167],{"class":147},[124,1232,1170],{"class":162},[124,1234,176],{"class":133},[124,1236,1237],{"class":126,"line":417},[124,1238,414],{"class":133},[124,1240,1241],{"class":126,"line":429},[124,1242,196],{"class":133},[13,1244,1245,1246,1248,1249,1252,1253,1256,1257,1260],{},"Apart from registering generators with Catch2, the ",[21,1247,68],{}," macro has\none more purpose, and that is to provide simple way of generating trivial\ngenerators, as seen in the first example on this page, where we used it\nas ",[21,1250,1251],{},"auto i = GENERATE(1, 2, 3);",". This usage converted each of the three\nliterals into a single ",[21,1254,1255],{},"SingleValueGenerator\u003Cint>"," and then placed them all in\na special generator that concatenates other generators. It can also be\nused with other generators as arguments, such as ",[21,1258,1259],{},"auto i = GENERATE(0, 2, take(100, random(300, 3000)));",". This is useful e.g. if you know that\nspecific inputs are problematic and want to test them separately\u002Ffirst.",[13,1262,1263],{},[16,1264,1265,1266,1268,1269,1272,1273,1000,1276,72],{},"For safety reasons, you cannot use variables inside the ",[21,1267,68],{}," macro.\nThis is done because the generator expression ",[102,1270,1271],{},"will"," outlive the outside\nscope and thus capturing references is dangerous. If you need to use\nvariables inside the generator expression, make sure you thought through\nthe lifetime implications and use ",[21,1274,1275],{},"GENERATE_COPY",[21,1277,1278],{},"GENERATE_REF",[10,1280,1281],{},[13,1282,1283,24,1285,1287],{},[21,1284,1275],{},[21,1286,1278],{}," were introduced in Catch2 2.7.1.",[13,1289,1290,1291,1294,1295,1298],{},"You can also override the inferred type by using ",[21,1292,1293],{},"as\u003Ctype>"," as the first\nargument to the macro. This can be useful when dealing with string literals,\nif you want them to come out as ",[21,1296,1297],{},"std::string",":",[115,1300,1302],{"className":117,"code":1301,"language":119,"meta":120,"style":120},"TEST_CASE(\"type conversion\", \"[generators]\") {\n    auto str = GENERATE(as\u003Cstd::string>{}, \"a\", \"bb\", \"ccc\");\n    REQUIRE(str.size() > 0);\n}\n",[21,1303,1304,1320,1365,1385],{"__ignoreMap":120},[124,1305,1306,1308,1310,1313,1315,1318],{"class":126,"line":127},[124,1307,23],{"class":130},[124,1309,134],{"class":133},[124,1311,1312],{"class":137},"\"type conversion\"",[124,1314,36],{"class":133},[124,1316,1317],{"class":137},"\"[generators]\"",[124,1319,141],{"class":133},[124,1321,1322,1324,1327,1329,1331,1334,1336,1339,1342,1345,1347,1350,1353,1355,1358,1360,1363],{"class":126,"line":144},[124,1323,148],{"class":147},[124,1325,1326],{"class":133}," str ",[124,1328,154],{"class":147},[124,1330,157],{"class":130},[124,1332,1333],{"class":133},"(as",[124,1335,405],{"class":147},[124,1337,1338],{"class":130},"std",[124,1340,1341],{"class":133},"::",[124,1343,1344],{"class":133},"string",[124,1346,1198],{"class":147},[124,1348,1349],{"class":133},"{}, ",[124,1351,1352],{"class":137},"\"a\"",[124,1354,36],{"class":133},[124,1356,1357],{"class":137},"\"bb\"",[124,1359,36],{"class":133},[124,1361,1362],{"class":137},"\"ccc\"",[124,1364,176],{"class":133},[124,1366,1367,1369,1372,1375,1378,1380,1383],{"class":126,"line":179},[124,1368,182],{"class":130},[124,1370,1371],{"class":133},"(str.",[124,1373,1374],{"class":130},"size",[124,1376,1377],{"class":133},"() ",[124,1379,1198],{"class":147},[124,1381,1382],{"class":162}," 0",[124,1384,176],{"class":133},[124,1386,1387],{"class":126,"line":193},[124,1388,196],{"class":133},[1390,1391,1393],"h3",{"id":1392},"random-number-generators-details","Random number generators: details",[10,1395,1396],{},[13,1397,1398,1399,72],{},"This section applies from Catch2 3.5.0. Before that, random generators\nwere a thin wrapper around distributions from ",[21,1400,1401],{},"\u003Crandom>",[13,1403,1404,1405,1408,1409,1412,1413,1416],{},"All of the ",[21,1406,1407],{},"random(a, b)"," generators in Catch2 currently generate uniformly\ndistributed number in closed interval [a; b]. This  is different from\n",[21,1410,1411],{},"std::uniform_real_distribution",", which should return numbers in interval\n[a; b) (but due to rounding can end up returning b anyway), but the\ndifference is intentional, so that ",[21,1414,1415],{},"random(a, a)"," makes sense. If there is\nenough interest from users, we can provide API to pick any of CC, CO, OC,\nor OO ranges.",[13,1418,1419,1420,1423,1424,1000,1427,72],{},"Unlike ",[21,1421,1422],{},"std::uniform_int_distribution",", Catch2's generators also support\nvarious single-byte integral types, such as ",[21,1425,1426],{},"char",[21,1428,1429],{},"bool",[1431,1432,1434],"h4",{"id":1433},"reproducibility","Reproducibility",[13,1436,1437],{},"Given the same seed, the output from the integral generators is fully\nreproducible across different platforms.",[13,1439,1440,1441,1444],{},"For floating point generators, the situation is much more complex.\nGenerally Catch2 only promises reproducibility (or even just correctness!)\non platforms that obey the IEEE-754 standard. Furthermore, reproducibility\nonly applies between binaries that perform floating point math in the\nsame way, e.g. if you compile a binary targeting the x87 FPU and another\none targeting SSE2 for floating point math, their results will vary.\nSimilarly, binaries compiled with compiler flags that relax the IEEE-754\nadherence, e.g. ",[21,1442,1443],{},"-ffast-math",", might provide different results than those\ncompiled for strict IEEE-754 adherence.",[13,1446,1447,1448,1451,1452,1454],{},"Finally, we provide zero guarantees on the reproducibility of generating\n",[21,1449,1450],{},"long double","s, as the internals of ",[21,1453,1450],{}," varies across different\nplatforms.",[297,1456,84],{"id":1457},"generator-interface",[13,1459,1460,1461,1463],{},"You can also implement your own generators, by deriving from the\n",[21,1462,294],{}," interface:",[115,1465,1467],{"className":117,"code":1466,"language":119,"meta":120,"style":120},"template\u003Ctypename T>\nstruct IGenerator : GeneratorUntypedBase {\n    \u002F\u002F via GeneratorUntypedBase:\n    \u002F\u002F Attempts to move the generator to the next element.\n    \u002F\u002F Returns true if successful (and thus has another element that can be read)\n    virtual bool next() = 0;\n\n    \u002F\u002F Precondition:\n    \u002F\u002F The generator is either freshly constructed or the last call to next() returned true\n    virtual T const& get() const = 0;\n\n    \u002F\u002F Returns user-friendly string showing the current generator element\n    \u002F\u002F Does not have to be overridden, IGenerator provides default implementation\n    virtual std::string stringifyImpl() const;\n\n    \u002F**\n     * Customization point for `skipToNthElement`\n     *\n     * Does not have to be overridden, there is a default implementation.\n     * Can be overridden for better performance.\n     *\n     * If there are not enough elements, shall throw an error.\n     *\n     * Going backwards is not supported.\n     *\u002F\n    virtual void skipToNthElementImpl( std::size_t n );\n\n    \u002F**\n     * Returns true if calls to `next` will eventually return false\n     *\n     * Note that for backwards compatibility this is currently defaulted\n     * to return `true`, but in the future all generators will have to\n     * provide their own implementation.\n     *\u002F\n    virtual bool isFinite() const = 0;\n};\n",[21,1468,1469,1485,1496,1502,1507,1512,1532,1538,1543,1548,1573,1577,1582,1588,1609,1614,1620,1626,1632,1638,1644,1649,1655,1660,1666,1672,1699,1704,1709,1715,1720,1726,1732,1738,1743,1763],{"__ignoreMap":120},[124,1470,1471,1474,1476,1479,1482],{"class":126,"line":127},[124,1472,1473],{"class":147},"template",[124,1475,405],{"class":133},[124,1477,1478],{"class":147},"typename",[124,1480,1481],{"class":130}," T",[124,1483,1484],{"class":133},">\n",[124,1486,1487,1490,1493],{"class":126,"line":144},[124,1488,1489],{"class":147},"struct",[124,1491,1492],{"class":130}," IGenerator",[124,1494,1495],{"class":133}," : GeneratorUntypedBase {\n",[124,1497,1498],{"class":126,"line":179},[124,1499,1501],{"class":1500},"sAwPA","    \u002F\u002F via GeneratorUntypedBase:\n",[124,1503,1504],{"class":126,"line":193},[124,1505,1506],{"class":1500},"    \u002F\u002F Attempts to move the generator to the next element.\n",[124,1508,1509],{"class":126,"line":396},[124,1510,1511],{"class":1500},"    \u002F\u002F Returns true if successful (and thus has another element that can be read)\n",[124,1513,1514,1517,1520,1523,1525,1527,1529],{"class":126,"line":411},[124,1515,1516],{"class":147},"    virtual",[124,1518,1519],{"class":147}," bool",[124,1521,1522],{"class":130}," next",[124,1524,1377],{"class":133},[124,1526,154],{"class":147},[124,1528,1382],{"class":162},[124,1530,1531],{"class":133},";\n",[124,1533,1534],{"class":126,"line":417},[124,1535,1537],{"emptyLinePlaceholder":1536},true,"\n",[124,1539,1540],{"class":126,"line":429},[124,1541,1542],{"class":1500},"    \u002F\u002F Precondition:\n",[124,1544,1545],{"class":126,"line":456},[124,1546,1547],{"class":1500},"    \u002F\u002F The generator is either freshly constructed or the last call to next() returned true\n",[124,1549,1550,1552,1555,1558,1561,1563,1566,1569,1571],{"class":126,"line":470},[124,1551,1516],{"class":147},[124,1553,1554],{"class":133}," T ",[124,1556,1557],{"class":147},"const&",[124,1559,1560],{"class":130}," get",[124,1562,1377],{"class":133},[124,1564,1565],{"class":147},"const",[124,1567,1568],{"class":147}," =",[124,1570,1382],{"class":162},[124,1572,1531],{"class":133},[124,1574,1575],{"class":126,"line":475},[124,1576,1537],{"emptyLinePlaceholder":1536},[124,1578,1579],{"class":126,"line":721},[124,1580,1581],{"class":1500},"    \u002F\u002F Returns user-friendly string showing the current generator element\n",[124,1583,1585],{"class":126,"line":1584},13,[124,1586,1587],{"class":1500},"    \u002F\u002F Does not have to be overridden, IGenerator provides default implementation\n",[124,1589,1591,1593,1596,1598,1600,1603,1605,1607],{"class":126,"line":1590},14,[124,1592,1516],{"class":147},[124,1594,1595],{"class":130}," std",[124,1597,1341],{"class":133},[124,1599,1344],{"class":130},[124,1601,1602],{"class":130}," stringifyImpl",[124,1604,1377],{"class":133},[124,1606,1565],{"class":147},[124,1608,1531],{"class":133},[124,1610,1612],{"class":126,"line":1611},15,[124,1613,1537],{"emptyLinePlaceholder":1536},[124,1615,1617],{"class":126,"line":1616},16,[124,1618,1619],{"class":1500},"    \u002F**\n",[124,1621,1623],{"class":126,"line":1622},17,[124,1624,1625],{"class":1500},"     * Customization point for `skipToNthElement`\n",[124,1627,1629],{"class":126,"line":1628},18,[124,1630,1631],{"class":1500},"     *\n",[124,1633,1635],{"class":126,"line":1634},19,[124,1636,1637],{"class":1500},"     * Does not have to be overridden, there is a default implementation.\n",[124,1639,1641],{"class":126,"line":1640},20,[124,1642,1643],{"class":1500},"     * Can be overridden for better performance.\n",[124,1645,1647],{"class":126,"line":1646},21,[124,1648,1631],{"class":1500},[124,1650,1652],{"class":126,"line":1651},22,[124,1653,1654],{"class":1500},"     * If there are not enough elements, shall throw an error.\n",[124,1656,1658],{"class":126,"line":1657},23,[124,1659,1631],{"class":1500},[124,1661,1663],{"class":126,"line":1662},24,[124,1664,1665],{"class":1500},"     * Going backwards is not supported.\n",[124,1667,1669],{"class":126,"line":1668},25,[124,1670,1671],{"class":1500},"     *\u002F\n",[124,1673,1675,1677,1680,1683,1686,1688,1690,1693,1696],{"class":126,"line":1674},26,[124,1676,1516],{"class":147},[124,1678,1679],{"class":147}," void",[124,1681,1682],{"class":130}," skipToNthElementImpl",[124,1684,1685],{"class":133},"( ",[124,1687,1338],{"class":130},[124,1689,1341],{"class":133},[124,1691,1692],{"class":147},"size_t",[124,1694,1695],{"class":1150}," n",[124,1697,1698],{"class":133}," );\n",[124,1700,1702],{"class":126,"line":1701},27,[124,1703,1537],{"emptyLinePlaceholder":1536},[124,1705,1707],{"class":126,"line":1706},28,[124,1708,1619],{"class":1500},[124,1710,1712],{"class":126,"line":1711},29,[124,1713,1714],{"class":1500},"     * Returns true if calls to `next` will eventually return false\n",[124,1716,1718],{"class":126,"line":1717},30,[124,1719,1631],{"class":1500},[124,1721,1723],{"class":126,"line":1722},31,[124,1724,1725],{"class":1500},"     * Note that for backwards compatibility this is currently defaulted\n",[124,1727,1729],{"class":126,"line":1728},32,[124,1730,1731],{"class":1500},"     * to return `true`, but in the future all generators will have to\n",[124,1733,1735],{"class":126,"line":1734},33,[124,1736,1737],{"class":1500},"     * provide their own implementation.\n",[124,1739,1741],{"class":126,"line":1740},34,[124,1742,1671],{"class":1500},[124,1744,1746,1748,1750,1753,1755,1757,1759,1761],{"class":126,"line":1745},35,[124,1747,1516],{"class":147},[124,1749,1519],{"class":147},[124,1751,1752],{"class":130}," isFinite",[124,1754,1377],{"class":133},[124,1756,1565],{"class":147},[124,1758,1568],{"class":147},[124,1760,1382],{"class":162},[124,1762,1531],{"class":133},[124,1764,1766],{"class":126,"line":1765},36,[124,1767,1768],{"class":133},"};\n",[10,1770,1771],{},[13,1772,1773,1776],{},[21,1774,1775],{},"skipToNthElementImpl"," was added in Catch2 3.13.0",[10,1778,1779],{},[13,1780,1781,1776],{},[21,1782,1783],{},"isFinite",[13,1785,1786,1787,1789,1790,1793,1794,1796,1797,72],{},"However, to be able to use your custom generator inside ",[21,1788,68],{},", it\nwill need to be wrapped inside a ",[21,1791,1792],{},"GeneratorWrapper\u003CT>",".\n",[21,1795,1792],{}," is a value wrapper around a\n",[21,1798,1799],{},"Catch::Detail::unique_ptr\u003CIGenerator\u003CT>>",[13,1801,1802,1803,72],{},"For full example of implementing your own generator, look into Catch2's\nexamples, specifically\n",[45,1804,1807],{"href":1805,"rel":1806},"https:\u002F\u002Fgithub.com\u002FProgmasoft\u002Fcatch3\u002Fblob\u002Fdevel\u002Fexamples\u002F300-Gen-OwnGenerator.cpp",[739],"Generators: Create your own generator",[1390,1809,1811],{"id":1810},"handling-empty-generators","Handling empty generators",[13,1813,1814],{},"The generator interface assumes that a generator always has at least one\nelement. This is not always true, e.g. if the generator depends on an external\ndatafile, the file might be missing.",[13,1816,1817],{},"There are two ways to handle this, depending on whether you want this\nto be an error or not.",[749,1819,1820,1827],{},[752,1821,1822,1823,1826],{},"If empty generator ",[16,1824,1825],{},"is"," an error, throw an exception in constructor.",[752,1828,1822,1829,1832,1833,1840],{},[16,1830,1831],{},"is not"," an error, use the ",[45,1834,1836,1839],{"href":1835},"\u002Fdocs\u002Fskipping-passing-failing\u002F#skipping-test-cases-at-runtime",[21,1837,1838],{},"SKIP"," macro"," in constructor.",[297,1842,90],{"id":1843},"other-usage-examples",[1390,1845,1847],{"id":1846},"adding-a-reproducer-to-random-tests","Adding a reproducer to random tests",[13,1849,1850],{},"If you use generators to generate random inputs for testing, you might\nwant to combine them with specific inputs, e.g. reproducers for previously\nfound issues.",[13,1852,1853,1854,1856],{},"Because ",[21,1855,68],{}," accepts multiple values\u002Fgenerators, the basic case is simple:",[115,1858,1860],{"className":117,"code":1859,"language":119,"meta":120,"style":120},"const int input = GENERATE(1, 2, take(10, random(10, 10'000'000)));\n",[21,1861,1862],{"__ignoreMap":120},[124,1863,1864,1866,1869,1872,1874,1876,1878,1880,1882,1884,1886,1888,1890,1893,1895,1897,1899,1901,1903,1906],{"class":126,"line":127},[124,1865,1565],{"class":147},[124,1867,1868],{"class":147}," int",[124,1870,1871],{"class":133}," input ",[124,1873,154],{"class":147},[124,1875,157],{"class":130},[124,1877,134],{"class":133},[124,1879,163],{"class":162},[124,1881,36],{"class":133},[124,1883,253],{"class":162},[124,1885,36],{"class":133},[124,1887,1131],{"class":130},[124,1889,134],{"class":133},[124,1891,1892],{"class":162},"10",[124,1894,36],{"class":133},[124,1896,1176],{"class":130},[124,1898,134],{"class":133},[124,1900,1892],{"class":162},[124,1902,36],{"class":133},[124,1904,1905],{"class":162},"10'000'000",[124,1907,1908],{"class":133},")));\n",[13,1910,1911,1912,1915],{},"This will set ",[21,1913,1914],{},"input"," first to \"1\", then to \"2\", and then to 10 random\nintegers.",[13,1917,1918,1919,1922,1923,1925,1926,1928],{},"But if you process the random inputs further (e.g. via ",[21,1920,1921],{},"map","), you can't\nrely on ",[21,1924,68],{},"'s support for multiple generators. In that case, you\nhave to use the ",[21,1927,1080],{}," generator combinator.",[115,1930,1932],{"className":117,"code":1931,"language":119,"meta":120,"style":120},"const auto input = GENERATE(\n    map( foo,\n         cat( value( 4 ), take( 10, random( 10, 10'000'000 ) ) ) ) );\n",[21,1933,1934,1950,1958],{"__ignoreMap":120},[124,1935,1936,1938,1941,1943,1945,1947],{"class":126,"line":127},[124,1937,1565],{"class":147},[124,1939,1940],{"class":147}," auto",[124,1942,1871],{"class":133},[124,1944,154],{"class":147},[124,1946,157],{"class":130},[124,1948,1949],{"class":133},"(\n",[124,1951,1952,1955],{"class":126,"line":144},[124,1953,1954],{"class":130},"    map",[124,1956,1957],{"class":133},"( foo,\n",[124,1959,1960,1963,1965,1968,1970,1972,1975,1977,1979,1981,1983,1985,1987,1989,1991,1993],{"class":126,"line":179},[124,1961,1962],{"class":130},"         cat",[124,1964,1685],{"class":133},[124,1966,1967],{"class":130},"value",[124,1969,1685],{"class":133},[124,1971,275],{"class":162},[124,1973,1974],{"class":133}," ), ",[124,1976,1131],{"class":130},[124,1978,1685],{"class":133},[124,1980,1892],{"class":162},[124,1982,36],{"class":133},[124,1984,1176],{"class":130},[124,1986,1685],{"class":133},[124,1988,1892],{"class":162},[124,1990,36],{"class":133},[124,1992,1905],{"class":162},[124,1994,1995],{"class":133}," ) ) ) ) );\n",[13,1997,1911,1998,2000,2001,2004,2005,72],{},[21,1999,1914],{}," first to ",[21,2002,2003],{},"foo(4)",", before transforming the 10 random\nintegers through ",[21,2006,2007],{},"foo",[2009,2010,2011],"style",{},"html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}",{"title":120,"searchDepth":144,"depth":144,"links":2013},[2014,2016,2019,2022],{"id":299,"depth":144,"text":2015},"Combining GENERATE and SECTION.",{"id":744,"depth":144,"text":78,"children":2017},[2018],{"id":1392,"depth":179,"text":1393},{"id":1457,"depth":144,"text":84,"children":2020},[2021],{"id":1810,"depth":179,"text":1811},{"id":1843,"depth":144,"text":90,"children":2023},[2024],{"id":1846,"depth":179,"text":1847},"md",{},"\u002Fdocs\u002Fgenerators",{"title":5,"description":120},"generators","docs\u002Fgenerators.md","docs\u002Fgenerators","dzvLx1m-HWQdX8OjOLKzsBYvA-F1W8ZhlMWUA-W7Luo",1794961594288]