Skip to content

Commit

Permalink
test build on MacOs
Browse files Browse the repository at this point in the history
  • Loading branch information
qindazhu committed Apr 25, 2020
1 parent 49759d2 commit 1036d7b
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
- name: Test
shell: bash
working-directory: ${{runner.workspace}}/build
run: ctest --build-config $BUILD_TYPE
run: CTEST_OUTPUT_ON_FAILURE=1 ctest --build-config $BUILD_TYPE
20 changes: 12 additions & 8 deletions k2/csrc/properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

#include "k2/csrc/properties.h"

#include <vector>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <unordered_set>
#include <vector>

#include "k2/csrc/fsa.h"

Expand Down Expand Up @@ -98,15 +99,18 @@ bool IsConnected(const Fsa &fsa) {
accessible.end())
return false;

std::cout << "accessible done........." << std::endl;
// reuse `accessible` to process co-accessible situation.
std::fill(accessible.begin(), accessible.end(), false);
accessible[num_states - 1] = true; // the final state
std::vector<bool> co_accessible(num_states, false);
co_accessible[num_states - 1] = true; // the final state
for (int32_t i = num_states - 1; i >= 0; --i) {
const auto &arc = fsa.arcs[i];
if (!accessible[arc.dest_state]) return false;
accessible[arc.src_state] = true;
if (!co_accessible[arc.dest_state]) return false;
co_accessible[arc.src_state] = true;
}
return std::find(accessible.begin(), accessible.end(), false) ==
accessible.end();
std::cout << "co-accessible done........." << std::endl;
// reuse `accessible` to process co-accessible situation.
return std::find(co_accessible.begin(), co_accessible.end(), false) ==
co_accessible.end();
}
} // namespace k2
176 changes: 145 additions & 31 deletions k2/csrc/properties_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ TEST(Properties, IsNotValid) {
// only epsilon arcs enter the final state
{
Fsa fsa;
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 1}, {1, 2, 0}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 1},
{1, 2, 0},
};
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, {3, 3}};
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -42,8 +46,17 @@ TEST(Properties, IsNotValid) {
// every state contains at least one arc except the final state
{
Fsa fsa;
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {2, 3, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 2}, {2, 3}, {3, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{2, 3, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 2},
{2, 3},
{3, 3},
};
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
bool is_valid = IsValid(fsa);
Expand All @@ -53,8 +66,15 @@ TEST(Properties, IsNotValid) {
// every state contains at least one arc except the final state (another case)
{
Fsa fsa;
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 2}, {2, 2}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 2},
{2, 2},
};
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
bool is_valid = IsValid(fsa);
Expand All @@ -64,8 +84,16 @@ TEST(Properties, IsNotValid) {
// `leaving_arcs` and `arcs` in this state are not consistent
{
Fsa fsa;
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {3, 3}, {2, 3}, {3, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{3, 3},
{2, 3},
{3, 3},
};
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
bool is_valid = IsValid(fsa);
Expand All @@ -75,8 +103,16 @@ TEST(Properties, IsNotValid) {
// `leaving_arcs` and `arcs` in this state are not consistent (another case)
{
Fsa fsa;
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {1, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, {4, 4}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{1, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
{4, 4},
};
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
bool is_valid = IsValid(fsa);
Expand All @@ -93,7 +129,11 @@ TEST(Properties, IsValid) {
}

{
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {1, 2, 0}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{1, 2, 0},
};
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, {3, 3}};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
Expand All @@ -104,8 +144,15 @@ TEST(Properties, IsValid) {
}

TEST(Properties, IsNotTopSorted) {
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {2, 1, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{2, 1, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -114,8 +161,15 @@ TEST(Properties, IsNotTopSorted) {
}

TEST(Properties, IsTopSorted) {
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {1, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{1, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -124,8 +178,15 @@ TEST(Properties, IsTopSorted) {
}

TEST(Properties, HasNoSelfLoops) {
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, {1, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
{1, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -134,8 +195,15 @@ TEST(Properties, HasNoSelfLoops) {
}

TEST(Properties, HasSelfLoops) {
std::vector<Arc> arcs = {{0, 1, 0}, {1, 2, 0}, {1, 1, 0}, };
std::vector<Range> leaving_arcs = {{0, 1}, {1, 3}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{1, 2, 0},
{1, 1, 0},
};
std::vector<Range> leaving_arcs = {
{0, 1},
{1, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -144,8 +212,15 @@ TEST(Properties, HasSelfLoops) {
}

TEST(Properties, IsNotDeterministic) {
std::vector<Arc> arcs = {{0, 1, 2}, {1, 2, 0}, {1, 3, 0}, };
std::vector<Range> leaving_arcs = {{0, 1}, {1, 3}, };
std::vector<Arc> arcs = {
{0, 1, 2},
{1, 2, 0},
{1, 3, 0},
};
std::vector<Range> leaving_arcs = {
{0, 1},
{1, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -154,8 +229,15 @@ TEST(Properties, IsNotDeterministic) {
}

TEST(Properties, IsDeterministic) {
std::vector<Arc> arcs = {{0, 1, 2}, {1, 2, 0}, {1, 3, 2}, };
std::vector<Range> leaving_arcs = {{0, 1}, {1, 3}, };
std::vector<Arc> arcs = {
{0, 1, 2},
{1, 2, 0},
{1, 3, 2},
};
std::vector<Range> leaving_arcs = {
{0, 1},
{1, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -164,8 +246,15 @@ TEST(Properties, IsDeterministic) {
}

TEST(Properties, IsNotEpsilonFree) {
std::vector<Arc> arcs = {{0, 1, 2}, {0, 2, 0}, {1, 2, 1}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
std::vector<Arc> arcs = {
{0, 1, 2},
{0, 2, 0},
{1, 2, 1},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -174,8 +263,15 @@ TEST(Properties, IsNotEpsilonFree) {
}

TEST(Properties, IsEpsilonFree) {
std::vector<Arc> arcs = {{0, 1, 2}, {0, 2, 1}, {1, 2, 1}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, };
std::vector<Arc> arcs = {
{0, 1, 2},
{0, 2, 1},
{1, 2, 1},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 3},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -186,8 +282,14 @@ TEST(Properties, IsEpsilonFree) {
TEST(Properties, IsNoConnected) {
// state is not accessible
{
std::vector<Arc> arcs = {{0, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 1}, {1, 1}, {1, 1}, };
std::vector<Arc> arcs = {
{0, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 1},
{1, 1},
{1, 1},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -197,8 +299,15 @@ TEST(Properties, IsNoConnected) {

// state is not co-accessible
{
std::vector<Arc> arcs = {{0, 1, 0}, {0, 2, 0}, };
std::vector<Range> leaving_arcs = {{0, 2}, {2, 2}, {2, 2}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 2, 0},
};
std::vector<Range> leaving_arcs = {
{0, 2},
{2, 2},
{2, 2},
};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
fsa.arcs = std::move(arcs);
Expand All @@ -208,7 +317,12 @@ TEST(Properties, IsNoConnected) {
}

TEST(Properties, IsConnected) {
std::vector<Arc> arcs = {{0, 1, 0}, {0, 3, 0}, {1, 2, 0}, {2, 3, 0}, };
std::vector<Arc> arcs = {
{0, 1, 0},
{0, 3, 0},
{1, 2, 0},
{2, 3, 0},
};
std::vector<Range> leaving_arcs = {{0, 2}, {2, 3}, {3, 4}, {4, 4}};
Fsa fsa;
fsa.leaving_arcs = std::move(leaving_arcs);
Expand Down

0 comments on commit 1036d7b

Please sign in to comment.