1 //===--- LRTableTest.cpp - ---------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang-pseudo/LRTable.h"
10 #include "clang-pseudo/Grammar.h"
11 #include "clang/Basic/TokenKinds.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
14 #include <vector>
15 
16 namespace clang {
17 namespace pseudo {
18 namespace {
19 
20 using testing::IsEmpty;
21 using testing::UnorderedElementsAre;
22 using Action = LRTable::Action;
23 
24 TEST(LRTable, Builder) {
25   GrammarTable GTable;
26 
27   //           eof   semi  ...
28   // +-------+----+-------+---
29   // |state0 |    | s0,r0 |...
30   // |state1 | acc|       |...
31   // |state2 |    |  r1   |...
32   // +-------+----+-------+---
33   std::vector<LRTable::Entry> Entries = {
34       {/* State */ 0, tokenSymbol(tok::semi), Action::shift(0)},
35       {/* State */ 0, tokenSymbol(tok::semi), Action::reduce(0)},
36       {/* State */ 1, tokenSymbol(tok::eof), Action::accept(2)},
37       {/* State */ 2, tokenSymbol(tok::semi), Action::reduce(1)}};
38   GrammarTable GT;
39   LRTable T = LRTable::buildForTests(GT, Entries);
40   EXPECT_THAT(T.find(0, tokenSymbol(tok::eof)), IsEmpty());
41   EXPECT_THAT(T.find(0, tokenSymbol(tok::semi)),
42               UnorderedElementsAre(Action::shift(0), Action::reduce(0)));
43   EXPECT_THAT(T.find(1, tokenSymbol(tok::eof)),
44               UnorderedElementsAre(Action::accept(2)));
45   EXPECT_THAT(T.find(1, tokenSymbol(tok::semi)), IsEmpty());
46   EXPECT_THAT(T.find(2, tokenSymbol(tok::semi)),
47               UnorderedElementsAre(Action::reduce(1)));
48   // Verify the behaivor for other non-available-actions terminals.
49   EXPECT_THAT(T.find(2, tokenSymbol(tok::kw_int)), IsEmpty());
50 }
51 
52 } // namespace
53 } // namespace pseudo
54 } // namespace clang
55