Enumerator generator

Aperta
#3,000 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
testing

Direzione di ricerca

Inizia con le macro GENERATE e GENERATE_REF di Catch2 e il generatore di intervalli, quindi confronta il loro comportamento con il ciclo su expected_results. Traccia come i valori generati vengono esposti a CAPTURE e determina se l’API dei generatori esistente può fornire sia un elemento sia il relativo indice senza indicizzazione manuale; il lavoro è completato quando l’approccio appropriato è documentato o implementato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
C++
Stelle
21.6k
Fork
3.4k
Merge medio
3g 12h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di catchorg/Catch2

Tutte le issue di catchorg/Catch2

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.