barelymusician/algorithm/context_free_grammar_test.cpp
// Tests that the context free grammar generation always returns a sequence with
// the expected size range when rules with multiple substitutions are present.
Tests that the context free grammar generation always returns a sequence with
the expected size range when rules with multiple substitutions are present.
Tests that the context free grammar generation always returns a sequence with the expected size range when rules with multiple substitutions are present .
TEST(ContextFreeGrammarTest, GenerateSequenceExpectedSizeRange) {
  const int kNumGenerations = 10;
  const int kStartSymbol = 0;
  const int kEndSymbol = -1;
  const int kMinSize = 2;
  const int kMaxSize = 5;
  ContextFreeGrammar<int> grammar;
  std::vector<std::vector<int>> substitutions;
  for (int i = kMinSize; i <= kMaxSize; ++i) {
    substitutions.push_back({i});
  }
  grammar.AddRule(kStartSymbol, substitutions);
  for (int i = kMinSize; i <= kMaxSize; ++i) {
    grammar.AddRule(i, {std::vector<int>(i, kEndSymbol)});
  }
  Random random;
  for (int n = 0; n < kNumGenerations; ++n) {
    const auto sequence = grammar.GenerateSequence(kStartSymbol, random);
    EXPECT_GE(sequence.size(), kMinSize);
    EXPECT_LE(sequence.size(), kMaxSize);
  }
}
TEST ( ContextFreeGrammarTest , GenerateSequenceExpectedSizeRange ) { const int kNumGenerations = 10 ; const int kStartSymbol = 0 ; const int kEndSymbol = -1 ; const int kMinSize = 2 ; const int kMaxSize = 5 ; ContextFreeGrammar < int > grammar ; std :: vector < std :: vector < int > > substitutions ; for ( int i = kMinSize ; i <= kMaxSize ; ++ i ) { substitutions . push_back ( { i } ) ; } grammar . AddRule ( kStartSymbol , substitutions ) ; for ( int i = kMinSize ; i <= kMaxSize ; ++ i ) { grammar . AddRule ( i , { std :: vector < int > ( i , kEndSymbol ) } ) ; } Random random ; for ( int n = 0 ; n < kNumGenerations ; ++ n ) { const auto sequence = grammar . GenerateSequence ( kStartSymbol , random ) ; EXPECT_GE ( sequence . size ( ) , kMinSize ) ; EXPECT_LE ( sequence . size ( ) , kMaxSize ) ; } }
Tests that the context free grammar generation always returns a sequence with
the expected size range when rules with multiple substitutions are present.
Tests that the context free grammar generation always returns a sequence with the expected size range when rules with multiple substitutions are present .