Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/GAP/pkg/semigroups/libsemigroups/tests/   (GAP Algebra Version 4.15.1©)  Datei vom 18.5.2025 mit Größe 44 kB image not shown  

Quelle  test-stephen.cpp

  Sprache: C
 

//
// libsemigroups - C++ library for semigroups and monoids
// Copyright (C) 2022 James D. Mitchell
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#define CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER

#include <cstddef>   // for size_t
#include <iostream>  // for cout

#include <algorithm>    // for for_each
#include <chrono>       // for duration, microsec...
#include <cstddef>      // for size_t
#include <cstdint>      // for size_t
#include <iostream>     // for string, char_traits
#include <iterator>     // for advance
#include <string>       // for swap, basic_string
#include <type_traits>  // for remove_reference<>...
#include <utility>      // for move
#include <vector>       // for vector, operator==

#include "catch.hpp"      // for REQUIRE, REQUIRE_THROWS_AS, REQUI...
#include "test-main.hpp"  // for LIBSEMIGROUPS_TEST_CASE

#include "libsemigroups/constants.hpp"             // for UNDEFINED
#include "libsemigroups/digraph-helper.hpp"        // for make, last_node_on...
#include "libsemigroups/digraph.hpp"               // for ActionDigraph, ope...
#include "libsemigroups/exception.hpp"             // for LibsemigroupsExcep...
#include "libsemigroups/fpsemi-examples.hpp"       // for make, fibonacci_se...
#include "libsemigroups/int-range.hpp"             // for IntegralRange<>::v...
#include "libsemigroups/iterator.hpp"              // for ConstIteratorState...
#include "libsemigroups/order.hpp"                 // for LexicographicalCom...
#include "libsemigroups/present.hpp"               // for add_rule_and_check
#include "libsemigroups/report.hpp"                // for ReportGuard
#include "libsemigroups/stephen.hpp"               // for Stephen, Stephen::...
#include "libsemigroups/todd-coxeter-digraph.hpp"  // for ToddCoxeterDigraph
#include "libsemigroups/types.hpp"                 // for word_type
#include "libsemigroups/word.hpp"                  // for StringToWord, word...

namespace libsemigroups {
  namespace {
    void check_000(Stephen& s) {
      s.set_word({0}).run();
      REQUIRE(s.word_graph().number_of_nodes() == 2);
      REQUIRE(s.word_graph()
              == action_digraph_helper::make<size_t>(
                  2, {{1, UNDEFINED}, {UNDEFINED, 1}}));
      REQUIRE(stephen::number_of_words_accepted(s) == POSITIVE_INFINITY);
      {
        auto first = stephen::cbegin_words_accepted(s);
        auto last  = stephen::cbegin_words_accepted(s);
        std::advance(last, 10);
        REQUIRE(std::vector<word_type>(first, last)
                == std::vector<word_type>({{0},
                                           {01},
                                           {011},
                                           {0111},
                                           {01111},
                                           {011111},
                                           {0111111},
                                           {01111111},
                                           {011111111},
                                           {0111111111}}));
      }
      {
        auto first = stephen::cbegin_left_factors(s);
        auto last  = stephen::cbegin_left_factors(s);
        std::advance(last, 10);
        REQUIRE(std::vector<word_type>(first, last)
                == std::vector<word_type>({{},
                                           {0},
                                           {01},
                                           {011},
                                           {0111},
                                           {01111},
                                           {011111},
                                           {0111111},
                                           {01111111},
                                           {011111111}}));
      }
    }

    void verify_c4_normal_form(Presentation<std::string> const& p,
                               std::string const&               word,
                               std::string const&               nf) {
      detail::StringToWord string_to_word(p.alphabet());
      Stephen              S(p);
      S.set_word(string_to_word(word)).run();

      auto words = std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                          stephen::cend_words_accepted(S));
      std::vector<std::string> strings(words.size(), "");

      std::transform(
          words.cbegin(), words.cend(), strings.begin(), [&p](auto const&&nbsp;w) {
            return detail::word_to_string(p.alphabet(), w.cbegin(), w.cend());
          });

      std::sort(strings.begin(),
                strings.end(),
                LexicographicalCompare<std::string>());
      REQUIRE(strings.front() == nf);

      REQUIRE(std::all_of(strings.cbegin(),
                          strings.cend(),
                          [&S, &string_to_word](auto const& w) {
                            return stephen::accepts(S, string_to_word(w));
                          }));
      REQUIRE(stephen::number_of_words_accepted(S) == strings.size());
    }

    void verify_c4_equal_to(Presentation<std::string> const& p,
                            std::string const&               word1,
                            std::string const&               word2) {
      detail::StringToWord string_to_word(p.alphabet());
      Stephen              S(p);
      S.set_word(string_to_word(word1)).run();
      REQUIRE(stephen::accepts(S, string_to_word(word2)));
      S.set_word(string_to_word(word2)).run();
      REQUIRE(stephen::accepts(S, string_to_word(word1)));
    }

    void verify_c4_not_equal_to(Presentation<std::string> const& p,
                                std::string const&               word1,
                                std::string const&               word2) {
      detail::StringToWord string_to_word(p.alphabet());
      Stephen              S(p);
      S.set_word(string_to_word(word1)).run();
      REQUIRE(!stephen::accepts(S, string_to_word(word2)));
      S.set_word(string_to_word(word2)).run();
      REQUIRE(!stephen::accepts(S, string_to_word(word1)));
    }

  }  // namespace

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "000",
                          "basic test 1",
                          "[quick][stephen]") {
    auto                    rg = ReportGuard(true);
    Presentation<word_type> p;
    p.alphabet(2);
    presentation::add_rule_and_check(p, {0}, {01});
    Stephen s;
    s.init(p);
    check_000(s);
    s.init(p);
    check_000(s);
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "001",
                          "basic test 2",
                          "[quick][stephen]") {
    auto                    rg = ReportGuard(true);
    Presentation<word_type> p;
    p.alphabet(2);
    presentation::add_rule_and_check(p, {000}, {0});
    presentation::add_rule_and_check(p, {111}, {1});
    presentation::add_rule_and_check(p, {0101}, {00});
    Stephen s(p);
    s.set_word({1101}).run();
    REQUIRE(s.word_graph().number_of_nodes() == 7);
    REQUIRE(s.word_graph()
            == action_digraph_helper::make<size_t>(7,
                                                   {{UNDEFINED, 1},
                                                    {UNDEFINED, 2},
                                                    {31},
                                                    {45},
                                                    {36},
                                                    {63},
                                                    {54}}));
    REQUIRE(stephen::number_of_words_accepted(s) == POSITIVE_INFINITY);

    word_type w = {1101};

    REQUIRE(action_digraph_helper::last_node_on_path_nc(
                s.word_graph(), 0, w.begin(), w.end())
                .first
            == 5);
    w = {110010};
    REQUIRE(action_digraph_helper::last_node_on_path_nc(
                s.word_graph(), 0, w.begin(), w.end())
                .first
            == 5);

    REQUIRE(stephen::accepts(s, {110010}));
    REQUIRE(stephen::accepts(s, {110010}));
    REQUIRE(!stephen::accepts(s, {}));
    REQUIRE(!stephen::accepts(s, {0000000000}));
    REQUIRE(!stephen::accepts(s, {111}));
    {
      auto first = stephen::cbegin_words_accepted(s);
      auto last  = stephen::cbegin_words_accepted(s);
      std::advance(last, 10);
      REQUIRE(std::vector<word_type>(first, last)
              == std::vector<word_type>({{1101},
                                         {110001},
                                         {110010},
                                         {110100},
                                         {110111},
                                         {111101},
                                         {11000001},
                                         {11000010},
                                         {11000100},
                                         {11000111}}));
    }
    {
      auto first = stephen::cbegin_left_factors(s);
      auto last  = stephen::cbegin_left_factors(s);
      std::advance(last, 10);
      REQUIRE(std::vector<word_type>(first, last)
              == std::vector<word_type>({{},
                                         {1},
                                         {11},
                                         {110},
                                         {111},
                                         {1100},
                                         {1101},
                                         {1111},
                                         {11000},
                                         {11001}}));
      REQUIRE(stephen::number_of_left_factors(s) == POSITIVE_INFINITY);
      std::for_each(first, last, [&s](auto const& w) {
        return stephen::is_left_factor(s, w);
      });
    }

    s.set_word({00}).run();
    REQUIRE(s.word_graph().number_of_nodes() == 5);
    REQUIRE(s.word_graph()
            == action_digraph_helper::make<size_t>(
                5, {{1, UNDEFINED}, {23}, {14}, {41}, {32}}));

    p.rules.clear();
    presentation::add_rule_and_check(p, {000}, {0});
    presentation::add_rule_and_check(p, {111}, {1});
    s.init(p).set_word({00}).run();
    REQUIRE(s.word_graph().number_of_nodes() == 3);
    REQUIRE(s.word_graph()
            == action_digraph_helper::make<size_t>(
                3, {{1, UNDEFINED}, {2, UNDEFINED}, {1, UNDEFINED}}));
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "002",
                          "full transf monoid",
                          "[quick][stephen]") {
    using namespace fpsemigroup;
    auto   rg = ReportGuard(true);
    size_t n  = 5;
    auto   p  = make<Presentation<word_type>>(
        full_transformation_monoid(n, author::Iwahori));
    presentation::replace_word(p, {}, {n});
    p.alphabet(n + 1);
    presentation::add_identity_rules(p, n);
    p.validate();

    Stephen s;
    s.init(std::move(p)).set_word({010111020120}).run();
    REQUIRE(s.word_graph().number_of_nodes() == 121);
    REQUIRE(s.word_graph()
            == action_digraph_helper::make<size_t>(
                121, {{1234, UNDEFINED, 5},
                      {5678, UNDEFINED, 1},
                      {951011, UNDEFINED, 2},
                      {1213514, UNDEFINED, 3},
                      {1516175, UNDEFINED, 4},
                      {1234, UNDEFINED, 5},
                      {1811920, UNDEFINED, 6},
                      {2122123, UNDEFINED, 7},
                      {2425261, UNDEFINED, 8},
                      {2182728, UNDEFINED, 9},
                      {2930231, UNDEFINED, 10},
                      {3233342, UNDEFINED, 11},
                      {3352136, UNDEFINED, 12},
                      {3733038, UNDEFINED, 13},
                      {3940413, UNDEFINED, 14},
                      {4424324, UNDEFINED, 15},
                      {4444533, UNDEFINED, 16},
                      {4647441, UNDEFINED, 17},
                      {694849, UNDEFINED, 18},
                      {5051652, UNDEFINED, 19},
                      {5354556, UNDEFINED, 20},
                      {7561257, UNDEFINED, 21},
                      {5875159, UNDEFINED, 22},
                      {6061627, UNDEFINED, 23},
                      {8636415, UNDEFINED, 24},
                      {6586654, UNDEFINED, 25},
                      {6768862, UNDEFINED, 26},
                      {5658969, UNDEFINED, 27},
                      {6365709, UNDEFINED, 28},
                      {10505671, UNDEFINED, 29},
                      {51101372, UNDEFINED, 30},
                      {73747510, UNDEFINED, 31},
                      {11537663, UNDEFINED, 32},
                      {54117716, UNDEFINED, 33},
                      {78791175, UNDEFINED, 34},
                      {48125080, UNDEFINED, 35},
                      {64816712, UNDEFINED, 36},
                      {13485882, UNDEFINED, 37},
                      {83777913, UNDEFINED, 38},
                      {14846064, UNDEFINED, 39},
                      {85147477, UNDEFINED, 40},
                      {62751417, UNDEFINED, 41},
                      {49158653, UNDEFINED, 42},
                      {57871560, UNDEFINED, 43},
                      {16498865, UNDEFINED, 44},
                      {89721674, UNDEFINED, 45},
                      {17905767, UNDEFINED, 46},
                      {91177279, UNDEFINED, 47},
                      {35371892, UNDEFINED, 48},
                      {42449318, UNDEFINED, 49},
                      {19293594, UNDEFINED, 50},
                      {30192295, UNDEFINED, 51},
                      {96979819, UNDEFINED, 52},
                      {20329942, UNDEFINED, 53},
                      {332010025, UNDEFINED, 54},
                      {1011022098, UNDEFINED, 55},
                      {272129103, UNDEFINED, 56},
                      {431044621, UNDEFINED, 57},
                      {222737105, UNDEFINED, 58},
                      {10610010222, UNDEFINED, 59},
                      {231073943, UNDEFINED, 60},
                      {1082397100, UNDEFINED, 61},
                      {41982326, UNDEFINED, 62},
                      {282410932, UNDEFINED, 63},
                      {361102439, UNDEFINED, 64},
                      {252811144, UNDEFINED, 65},
                      {112952597, UNDEFINED, 66},
                      {261133646, UNDEFINED, 67},
                      {1142695102, UNDEFINED, 68},
                      {10710811527, UNDEFINED, 69},
                      {11311428115, UNDEFINED, 70},
                      {10911211329, UNDEFINED, 71},
                      {116454730, UNDEFINED, 72},
                      {3196107109, UNDEFINED, 73},
                      {97314045, UNDEFINED, 74},
                      {115413134, UNDEFINED, 75},
                      {10310632107, UNDEFINED, 76},
                      {117383340, UNDEFINED, 77},
                      {34101103113, UNDEFINED, 78},
                      {102343847, UNDEFINED, 79},
                      {9911710135, UNDEFINED, 80},
                      {11136112117, UNDEFINED, 81},
                      {11011111437, UNDEFINED, 82},
                      {3899106110, UNDEFINED, 83},
                      {92399699, UNDEFINED, 84},
                      {4092108111, UNDEFINED, 85},
                      {941164296, UNDEFINED, 86},
                      {10543116106, UNDEFINED, 87},
                      {10410544108, UNDEFINED, 88},
                      {4594104112, UNDEFINED, 89},
                      {934694101, UNDEFINED, 90},
                      {4793105114, UNDEFINED, 91},
                      {848511848, UNDEFINED, 92},
                      {909149118, UNDEFINED, 93},
                      {86899050, UNDEFINED, 94},
                      {119666851, UNDEFINED, 95},
                      {52738486, UNDEFINED, 96},
                      {74526166, UNDEFINED, 97},
                      {118625255, UNDEFINED, 98},
                      {80835384, UNDEFINED, 99},
                      {120595461, UNDEFINED, 100},
                      {55788090, UNDEFINED, 101},
                      {79555968, UNDEFINED, 102},
                      {761207856, UNDEFINED, 103},
                      {885789120, UNDEFINED, 104},
                      {87889158, UNDEFINED, 105},
                      {59768387, UNDEFINED, 106},
                      {69607376, UNDEFINED, 107},
                      {61698588, UNDEFINED, 108},
                      {711196373, UNDEFINED, 109},
                      {826411983, UNDEFINED, 110},
                      {81826585, UNDEFINED, 111},
                      {66718189, UNDEFINED, 112},
                      {70677178, UNDEFINED, 113},
                      {68708291, UNDEFINED, 114},
                      {751186970, UNDEFINED, 115},
                      {728687119, UNDEFINED, 116},
                      {778012081, UNDEFINED, 117},
                      {981159293, UNDEFINED, 118},
                      {95109110116, UNDEFINED, 119},
                      {100103117104, UNDEFINED, 120}}));
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "003",
                          "from step_hen 002",
                          "[quick][stephen]") {
    Presentation<std::string> p;
    p.alphabet("ab");
    presentation::add_rule_and_check(p, "aaa""a");
    presentation::add_rule_and_check(p, "bbb""b");
    presentation::add_rule_and_check(p, "abab""aa");

    Stephen              S(p);
    detail::StringToWord string_to_word("ab");
    S.set_word(string_to_word("bbab"));

    REQUIRE(stephen::accepts(S, string_to_word("bbaaba")));
    REQUIRE(!stephen::accepts(S, string_to_word("")));
    REQUIRE(!stephen::accepts(S, string_to_word("aaaaaaaaaa")));
    REQUIRE(!stephen::accepts(S, string_to_word("bbb")));

    S.set_word(string_to_word("bba"));
    REQUIRE(stephen::accepts(S, string_to_word("bbabb")));
    REQUIRE(stephen::accepts(S, string_to_word("bba")));
    REQUIRE(!stephen::accepts(S, string_to_word("bbb")));
    REQUIRE(!stephen::accepts(S, string_to_word("a")));
    REQUIRE(!stephen::accepts(S, string_to_word("ab")));

    S.set_word(string_to_word("bbaab"));
    REQUIRE(stephen::accepts(S, string_to_word("bbaba")));
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "004",
                          "from step_hen 003",
                          "[quick][stephen]") {
    Presentation<std::string> p;
    p.alphabet("abcdefg");
    presentation::add_rule_and_check(p, "aaaeaa""abcd");
    presentation::add_rule_and_check(p, "ef""dg");

    Stephen              S(p);
    detail::StringToWord string_to_word("abcdefg");

    S.set_word(string_to_word("abcef")).run();
    REQUIRE(string_to_word("abcef") == word_type({01245}));
    REQUIRE(S.word_graph()
            == action_digraph_helper::make<size_t>(
                11,
                {{1,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED},
                 {23, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED, UNDEFINED},
                 {4,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED},
                 {UNDEFINED,
                  UNDEFINED,
                  5,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED},
                 {UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  6,
                  UNDEFINED,
                  UNDEFINED},
                 {UNDEFINED, UNDEFINED, UNDEFINED, 78, UNDEFINED, UNDEFINED},
                 {9,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED},
                 {UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  10},
                 {UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  10,
                  UNDEFINED},
                 {7,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED},
                 {UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED,
                  UNDEFINED}}));
    auto rule = string_to_word(p.rules[0]);
    auto m    = action_digraph_helper::last_node_on_path(
                 S.word_graph(), 0, rule.cbegin(), rule.cend())
                 .first;
    rule   = string_to_word(p.rules[1]);
    auto n = action_digraph_helper::last_node_on_path(
                 S.word_graph(), 0, rule.cbegin(), rule.cend())
                 .first;
    REQUIRE(m != UNDEFINED);
    REQUIRE(n != UNDEFINED);
    REQUIRE(m == n);
    REQUIRE(S.word_graph().number_of_nodes() == 11);
    REQUIRE(stephen::accepts(S, string_to_word("aaaeaag")));
    REQUIRE(stephen::number_of_words_accepted(S) == 3);
    REQUIRE(std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                   stephen::cend_words_accepted(S))
            == std::vector<word_type>(
                {{01236}, {01245}, {0004006}}));

    S.set_word(string_to_word("aaaeaaaeaa")).run();

    REQUIRE(S.word_graph().number_of_nodes() == 15);
    REQUIRE(stephen::number_of_words_accepted(S) == 3);
    REQUIRE(stephen::accepts(S, string_to_word("aaaeabcd")));
    REQUIRE(std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                   stephen::cend_words_accepted(S))
            == std::vector<word_type>({{00040123},
                                       {01230400},
                                       {0004000400}}));

    S.set_word(string_to_word("aaaeaag")).run();
    REQUIRE(S.word_graph().number_of_nodes() == 11);
    REQUIRE(stephen::number_of_words_accepted(S) == 3);
    REQUIRE(std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                   stephen::cend_words_accepted(S))
            == std::vector<word_type>(
                {{01236}, {01245}, {0004006}}));
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "005",
                          "from step_hen 004",
                          "[quick][stephen]") {
    Presentation<std::string> p;
    p.alphabet("abc");
    presentation::add_rule_and_check(p, "ab""ba");
    presentation::add_rule_and_check(p, "ac""cc");
    presentation::add_rule_and_check(p, "ac""a");
    presentation::add_rule_and_check(p, "cc""a");
    presentation::add_rule_and_check(p, "bc""cc");
    presentation::add_rule_and_check(p, "bcc""b");
    presentation::add_rule_and_check(p, "bc""b");
    presentation::add_rule_and_check(p, "cc""b");
    presentation::add_rule_and_check(p, "a""b");

    Stephen              S(p);
    detail::StringToWord string_to_word("abc");
    S.set_word(string_to_word("abcc")).run();
    REQUIRE(stephen::accepts(S, string_to_word("baac")));
    REQUIRE(S.word_graph().number_of_nodes() == 3);
    REQUIRE(stephen::number_of_words_accepted(S) == POSITIVE_INFINITY);
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "006",
                          "from step_hen 005",
                          "[quick][stephen]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "bb""c");
    presentation::add_rule_and_check(p, "caca""abab");
    presentation::add_rule_and_check(p, "bc""d");
    presentation::add_rule_and_check(p, "cb""d");
    presentation::add_rule_and_check(p, "aa""d");
    presentation::add_rule_and_check(p, "ad""a");
    presentation::add_rule_and_check(p, "da""a");
    presentation::add_rule_and_check(p, "bd""b");
    presentation::add_rule_and_check(p, "db""b");
    presentation::add_rule_and_check(p, "cd""c");
    presentation::add_rule_and_check(p, "dc""c");

    Stephen              S(p);
    detail::StringToWord string_to_word("abcd");
    S.set_word(string_to_word("dabdaaadabab")).run();
    REQUIRE(stephen::accepts(S, string_to_word("abdadcaca")));
    REQUIRE(S.word_graph().number_of_nodes() == 25);
    REQUIRE(stephen::number_of_words_accepted(S) == POSITIVE_INFINITY);
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "007",
                          "Fibonacci(4, 6)",
                          "[stephen][extreme]") {
    using namespace fpsemigroup;
    auto    rg = ReportGuard(true);
    auto    p  = make<Presentation<word_type>>(fibonacci_semigroup(46));
    Stephen S(p);
    S.set_word({0123}).run_for(std::chrono::seconds(10));
    REQUIRE(!S.finished());
  }

  LIBSEMIGROUPS_TEST_CASE(
      "Stephen",
      "008",
      "C(4) monoid normal form (test_case_knuth_bendix_055)",
      "[stephen][quick]") {
    auto                      rg = ReportGuard(false);
    Presentation<std::string> p;
    p.alphabet("abcdefg");
    presentation::add_rule_and_check(p, "abcd""ce");
    presentation::add_rule_and_check(p, "df""dg");

    detail::StringToWord string_to_word("abcdefg");
    Stephen              S(p);
    S.set_word(string_to_word("dfabcdf")).run();

    REQUIRE(S.word_graph().number_of_nodes() == 9);
    REQUIRE(stephen::number_of_words_accepted(S) == 8);

    auto words = std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                        stephen::cend_words_accepted(S));
    std::vector<std::string> strings(words.size(), "");

    std::transform(
        words.cbegin(), words.cend(), strings.begin(), [&p](auto const&&nbsp;w) {
          return detail::word_to_string(p.alphabet(), w.cbegin(), w.cend());
        });

    REQUIRE(strings
            == std::vector<std::string>({"dfcef",
                                         "dfceg",
                                         "dgcef",
                                         "dgceg",
                                         "dfabcdf",
                                         "dfabcdg",
                                         "dgabcdf",
                                         "dgabcdg"}));
    std::sort(
        strings.begin(), strings.end(), LexicographicalCompare<std::string>());
    REQUIRE(strings.front() == "dfabcdf");

    REQUIRE(std::all_of(
        strings.cbegin(), strings.cend(), [&S, &string_to_word](auto const& w) {
          return stephen::accepts(S, string_to_word(w));
        }));
    REQUIRE(stephen::number_of_words_accepted(S) == strings.size());

    S.set_word(string_to_word("abcdfceg")).run();
    REQUIRE(stephen::number_of_words_accepted(S) == 16);
    words = std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                   stephen::cend_words_accepted(S));
    strings.resize(stephen::number_of_words_accepted(S));

    std::transform(
        words.cbegin(), words.cend(), strings.begin(), [&p](auto const&&nbsp;w) {
          return detail::word_to_string(p.alphabet(), w.cbegin(), w.cend());
        });

    std::sort(
        strings.begin(), strings.end(), LexicographicalCompare<std::string>());
    REQUIRE(strings
            == std::vector<std::string>({"abcdfabcdf",
                                         "abcdfabcdg",
                                         "abcdfcef",
                                         "abcdfceg",
                                         "abcdgabcdf",
                                         "abcdgabcdg",
                                         "abcdgcef",
                                         "abcdgceg",
                                         "cefabcdf",
                                         "cefabcdg",
                                         "cefcef",
                                         "cefceg",
                                         "cegabcdf",
                                         "cegabcdg",
                                         "cegcef",
                                         "cegceg"}));

    REQUIRE(strings.front() == "abcdfabcdf");
    REQUIRE(stephen::accepts(S, string_to_word("abcdfabcdf")));
  }

  LIBSEMIGROUPS_TEST_CASE(
      "Stephen",
      "009",
      "C(4) monoid normal form (test_case_gap_smalloverlap_85)",
      "[stephen][quick]") {
    auto                      rg = ReportGuard(false);
    Presentation<std::string> p;
    p.alphabet("cab");
    presentation::add_rule_and_check(p, "aabc""acba");

    detail::StringToWord string_to_word("cab");
    Stephen              S(p);
    S.set_word(string_to_word("a")).run();
    REQUIRE(!stephen::accepts(S, string_to_word("b")));

    S.set_word(string_to_word("aabcabc")).run();
    REQUIRE(stephen::accepts(S, string_to_word("aabccba")));

    S.set_word(string_to_word("aabccba")).run();
    REQUIRE(stephen::accepts(S, string_to_word("aabcabc")));

    S.set_word(string_to_word("acba")).run();
    auto words = std::vector<word_type>(stephen::cbegin_words_accepted(S),
                                        stephen::cend_words_accepted(S));
    std::vector<std::string> strings(words.size(), "");

    std::transform(
        words.cbegin(), words.cend(), strings.begin(), [&p](auto const&&nbsp;w) {
          return detail::word_to_string(p.alphabet(), w.cbegin(), w.cend());
        });

    REQUIRE(strings == std::vector<std::string>({"acba""aabc"}));
    verify_c4_normal_form(p, "acba""aabc");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "010",
                          "code coverage",
                          "[stephen][quick]") {
    auto                      rg = ReportGuard(true);
    Presentation<std::string> p;
    REQUIRE_THROWS_AS(Stephen(p), LibsemigroupsException);
    p.alphabet("abcdefg");

    detail::StringToWord string_to_word("abcdefg");
    Stephen              S(p);
    auto                 w = string_to_word("abbbddbcbcbc");
    S.set_word(w);
    S.run();
    REQUIRE(S.finished());
    S.run();
    S.set_word(string_to_word("abbbddbcbcbc"));  // resets
    S.report_every(std::chrono::microseconds(10));

    S.run();

    Stephen T(S);
    REQUIRE(stephen::accepts(T, w));
    REQUIRE(!stephen::accepts(T, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(T) == 1);
    REQUIRE(stephen::number_of_left_factors(T) == w.size() + 1);
    REQUIRE(stephen::accepts(S, w));
    REQUIRE(!stephen::accepts(S, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(S) == 1);
    REQUIRE(stephen::number_of_left_factors(S) == w.size() + 1);

    Stephen U(std::move(S));
    REQUIRE(stephen::accepts(U, w));
    REQUIRE(!stephen::accepts(U, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(U) == 1);
    REQUIRE(stephen::number_of_left_factors(U) == w.size() + 1);

    S = T;
    REQUIRE(stephen::accepts(T, w));
    REQUIRE(!stephen::accepts(T, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(T) == 1);
    REQUIRE(stephen::number_of_left_factors(T) == w.size() + 1);
    REQUIRE(stephen::accepts(S, w));
    REQUIRE(!stephen::accepts(S, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(S) == 1);
    REQUIRE(stephen::number_of_left_factors(S) == w.size() + 1);

    Stephen V;
    V = std::move(S);
    REQUIRE(stephen::accepts(V, w));
    REQUIRE(!stephen::accepts(V, string_to_word("abbbd")));
    REQUIRE(stephen::number_of_words_accepted(V) == 1);
    REQUIRE(stephen::number_of_left_factors(V) == w.size() + 1);
    REQUIRE(V.word() == w);
    REQUIRE(V.accept_state() == 12);
  }

  LIBSEMIGROUPS_TEST_CASE(
      "Stephen",
      "011",
      "C(4) monoid normal form (test_case_gap_smalloverlap_49)",
      "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcdefgh");

    presentation::add_rule_and_check(p, "abcd""ce");
    presentation::add_rule_and_check(p, "df""hd");
    verify_c4_equal_to(p, "abchd""abcdf");
    verify_c4_equal_to(p, "abchd""abchd");
    verify_c4_equal_to(p, "abchdf""abchhd");
    verify_c4_equal_to(p, "abchd""cef");
    verify_c4_equal_to(p, "cef""abchd");
    verify_c4_not_equal_to(p, "abchf""abcdf");

    verify_c4_equal_to(p, "hdfabce""dffababcd");

    verify_c4_normal_form(p, "hdfabce""dffababcd");
  }

  LIBSEMIGROUPS_TEST_CASE(
      "Stephen",
      "012",
      "C(4) monoid normal form (test_case_gap_smalloverlap_63)",
      "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcdefgh");

    presentation::add_rule_and_check(p, "afh""bgh");
    presentation::add_rule_and_check(p, "hc""d");
    verify_c4_equal_to(p, "afd""bgd");
    verify_c4_equal_to(p, "bghcafhbgd""afdafhafd");
    verify_c4_normal_form(p, "bghcafhbgd""afdafhafd");
  }

  LIBSEMIGROUPS_TEST_CASE(
      "Stephen",
      "013",
      "C(4) monoid equal to (test_case_gap_smalloverlap_70)",
      "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcdefghij");

    presentation::add_rule_and_check(p, "afh""bgh");
    presentation::add_rule_and_check(p, "hc""de");
    presentation::add_rule_and_check(p, "ei""j");

    verify_c4_equal_to(p, "afdj""bgdj");
    verify_c4_not_equal_to(p, "xxxxxxxxxxxxxxxxxxxxxxx""b");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "014",
                          "C(4) monoid normal form (test_case_ex_3_13_14)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abbba""cdc");
    verify_c4_normal_form(p, "cdcdcabbbabbbabbcd""abbbadcabbbabbbabbcd");
    verify_c4_equal_to(p, "cdcdcabbbabbbabbcd""abbbadcabbbabbbabbcd");

    verify_c4_equal_to(p, "abbbadcbbba""cdabbbcdc");
    verify_c4_equal_to(p, "cdabbbcdc""cdabbbcdc");
    verify_c4_normal_form(p, "cdabbbcdc""abbbadcbbba");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "015",
                          "C(4) monoid normal form (test_case_ex_3_15)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "aabc""acba");
    std::string original = "cbacbaabcaabcacbacba";
    std::string expected = "cbaabcabcaabcaabcabc";

    verify_c4_equal_to(p, "cbaabcabcaabcaabccba", original);
    verify_c4_equal_to(p, original, expected);
    verify_c4_equal_to(p, expected, original);
    verify_c4_equal_to(p, "cbaabcabcaabcaabccba", expected);

    verify_c4_equal_to(p, original, "cbaabcabcaabcaabccba");

    verify_c4_equal_to(p, expected, "cbaabcabcaabcaabccba");
    verify_c4_normal_form(p, original, expected);
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "016",
                          "C(4) monoid normal form (test_case_ex_3_16)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abcd""acca");
    std::string original = "bbcabcdaccaccabcddd";
    std::string expected = "bbcabcdabcdbcdbcddd";

    verify_c4_equal_to(p, original, expected);
    verify_c4_equal_to(p, expected, original);

    verify_c4_normal_form(p, original, expected);
    verify_c4_normal_form(p, expected, expected);
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "017",
                          "C(4) monoid normal form (test_case_mt_3)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abcd""accca");

    verify_c4_normal_form(p, "bbcabcdaccaccabcddd""bbcabcdaccaccabcddd");
    verify_c4_equal_to(p, "bbcabcdaccaccabcddd""bbcabcdaccaccabcddd");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "018",
                          "C(4) monoid normal form (test_case_mt_5)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abc");
    presentation::add_rule_and_check(p, "ac""cbbbbc");

    verify_c4_normal_form(p, "acbbbbc""aac");
    verify_c4_equal_to(p, "acbbbbc""aac");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "019",
                          "C(4) monoid normal form (test_case_mt_6)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abc");
    presentation::add_rule_and_check(p, "ccab""cbac");

    verify_c4_normal_form(p, "bacbaccabccabcbacbac""bacbacbaccbaccbacbac");
    verify_c4_equal_to(p, "bacbaccabccabcbacbac""bacbacbaccbaccbacbac");
    verify_c4_normal_form(p, "ccabcbaccab""cbaccbacbac");
    verify_c4_equal_to(p, "ccabcbaccab""cbaccbacbac");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "020",
                          "C(4) monoid normal form (test_case_mt_10)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcdefghij");
    presentation::add_rule_and_check(p, "afh""bgh");
    presentation::add_rule_and_check(p, "hc""de");
    presentation::add_rule_and_check(p, "ei""j");

    verify_c4_normal_form(p, "bgdj""afdei");
    verify_c4_equal_to(p, "bgdj""afdei");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "021",
                          "C(4) monoid normal form (test_case_mt_13)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abcd""dcba");

    verify_c4_normal_form(p, "dcbdcba""abcdbcd");
    verify_c4_equal_to(p, "dcbdcba""abcdbcd");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "022",
                          "C(4) monoid normal form (test_case_mt_14)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abca""dcbd");

    verify_c4_normal_form(p, "dcbabca""abcacbd");
    verify_c4_equal_to(p, "dcbabca""abcacbd");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "023",
                          "C(4) monoid normal form (test_case_mt_15)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "abcd""dcba");
    presentation::add_rule_and_check(p, "adda""dbbd");

    verify_c4_normal_form(p, "dbbabcd""addacba");
    verify_c4_equal_to(p, "dbbabcd""addacba");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "024",
                          "C(4) monoid normal form (test_case_mt_16)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcdefg");
    presentation::add_rule_and_check(p, "abcd""acca");
    presentation::add_rule_and_check(p, "gf""ge");

    verify_c4_normal_form(p, "accabcdgf""abcdbcdge");
    verify_c4_equal_to(p, "accabcdgf""abcdbcdge");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "025",
                          "C(4) monoid normal form (test_case_mt_17)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(
        p, "ababbabbbabbbb""abbbbbabbbbbbabbbbbbbabbbbbbbb");
    presentation::add_rule_and_check(
        p, "cdcddcdddcdddd""cdddddcddddddcdddddddcdddddddd");

    verify_c4_normal_form(
        p, "abbbacdddddcddddddcdddddddcdddddddd""abbbacdcddcdddcdddd");
    verify_c4_equal_to(
        p, "abbbacdddddcddddddcdddddddcdddddddd""abbbacdcddcdddcdddd");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "026",
                          "C(4) monoid normal form (test_case_weak_1)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "acba""aabc");
    presentation::add_rule_and_check(p, "acba""dbbbd");

    verify_c4_equal_to(p, "aaabc""adbbbd");
    verify_c4_equal_to(p, "adbbbd""aaabc");

    verify_c4_equal_to(p, "aaabcadbbbd""adbbbdadbbbd");
    verify_c4_equal_to(p, "aaabcaaabc""adbbbdadbbbd");
    verify_c4_equal_to(p, "acba""dbbbd");
    verify_c4_equal_to(p, "acbabbbd""aabcbbbd");
    verify_c4_equal_to(p, "aabcbbbd""acbabbbd");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "027",
                          "C(4) monoid normal form (test_case_weak_2)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "acba""aabc");
    presentation::add_rule_and_check(p, "acba""adbd");
    verify_c4_equal_to(p, "acbacba""aabcabc");
    verify_c4_normal_form(p, "acbacba""aabcabc");
    verify_c4_equal_to(p, "aabcabc""acbacba");
    verify_c4_normal_form(p, "aabcabc""aabcabc");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "028",
                          "C(4) monoid normal form (test_case_weak_3)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcde");
    presentation::add_rule_and_check(p, "bceac""aeebbc");
    presentation::add_rule_and_check(p, "aeebbc""dabcd");
    verify_c4_normal_form(p, "bceacdabcd""aeebbcaeebbc");
    verify_c4_normal_form(p, "aeebbcaeebbc""aeebbcaeebbc");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "029",
                          "C(4) monoid normal form (test_case_weak_4)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "acba""aabc");
    presentation::add_rule_and_check(p, "acba""dbbd");
    verify_c4_normal_form(p, "bbacbcaaabcbbd""bbacbcaaabcbbd");
    verify_c4_normal_form(p, "acbacba""aabcabc");
    verify_c4_normal_form(p, "aabcabc""aabcabc");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "030",
                          "C(4) monoid normal form (test_case_weak_5)",
                          "[stephen][quick]") {
    Presentation<std::string> p;
    p.alphabet("abcd");
    presentation::add_rule_and_check(p, "acba""aabc");
    presentation::add_rule_and_check(p, "acba""adbd");
    verify_c4_normal_form(p, "acbacba""aabcabc");
    verify_c4_normal_form(p, "aabcabc""aabcabc");
  }

  LIBSEMIGROUPS_TEST_CASE("Stephen",
                          "031",
                          "Test behaviour when uninitialised",
                          "[stephen][quick]") {
    Stephen S;

    REQUIRE_THROWS_AS(S.accept_state(), LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::cbegin_words_accepted(S),
                      LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::cend_words_accepted(S), LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::cbegin_left_factors(S), LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::cend_left_factors(S), LibsemigroupsException);

    REQUIRE_THROWS_AS(stephen::is_left_factor(S, {000}),
                      LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::accepts(S, {000}), LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::number_of_left_factors(S),
                      LibsemigroupsException);
    REQUIRE_THROWS_AS(stephen::number_of_words_accepted(S),
                      LibsemigroupsException);
    REQUIRE_THROWS_AS(S.run(), LibsemigroupsException);
  }
}  // namespace libsemigroups

Messung V0.5 in Prozent
C=75 H=89 G=82

¤ Dauer der Verarbeitung: 0.24 Sekunden  (vorverarbeitet am  2026-06-17) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.