Enumerator generator

オープン
#3,000 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
42/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
cpp
領域
testing

調査の方向性

Catch2 の GENERATE および GENERATE_REF マクロと range generator から始め、その動作を expected_results に対するループと比較します。生成された値が CAPTURE にどのように公開されるかを追跡し、既存の generator API で手動のインデックス付けなしに要素とそのインデックスの両方を提供できるかを確認します。適切なアプローチを文書化または実装できれば完了です。

索引モデルが issue の本文から書いたものです。

説明

I'm using fff to mock a call and I have a std::vector of expected arguments for the mocked function. The way fff works, I need a generator that works like the Python enumerate statement: on each iteration it returns the iteration number and an element of my vector, so my test goes something like this:

REQUIRE(my_function_fake.arg0_history[i] == val_a);
REQUIRE(my_function_fake.arg1_history[i] == val_b);

I need both the vector element and the index. I'm trying to understand how to best go about it. My first try was to simply iterate over the vector and use CAPTURE:

uint32_t factorial(uint32_t number)
{
    return number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("Factorials are computed", "[factorial]")
{
    std::vector<std::tuple<uint32_t, uint32_t>> expected_results = {
        {1, 1},
        {2, 2},
        {3, 6},
        {10, 3'628'800+1},  // Bad value
    };

    for( int i = -1; const auto& v : expected_results ) {
        ++i;
        auto& [input, expected] = v;
        CAPTURE(i, input, expected);
        REQUIRE(factorial(input) == expected);
    }

This works great, I get a nice error message with the particular values that failed:

test.cc:50: FAILED:
  REQUIRE( factorial(input) == expected )
with expansion:
  3628800 (0x375f00) == 3628801 (0x375f01)
with messages:
  i := 3
  input := 10
  expected := 3628801 (0x375f01)

Using the range generator seems a bit overly complicated:

    size_t i = GENERATE_REF(Catch::Generators::range(static_cast<size_t>(0), expected_results.size()));
    auto [input, expected] = expected_results[i];
    CAPTURE(i, input, expected);
    REQUIRE(factorial(input) == expected);

Is there a way to use one of the GENERATE macros for this?

Thanks!

主要言語
C++
スター
21.6k
フォーク
3.4k
平均マージ
3日 12時間
マージ済み PR(30日)
5

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

catchorg/Catch2 のほかの issue

catchorg/Catch2 の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。