-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from bliutech/18-utils-add-a-benchmark-to-coun…
…t-the-number-of-boolean-operations 18 utils add a benchmark to count the number of boolean operations
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from unittest import TestCase | ||
|
||
from parser.ast import Expr | ||
from parser.parse import Parser | ||
from utils.metrics import OpCounter | ||
|
||
|
||
class TestCount(TestCase): | ||
def test_counter(self) -> None: | ||
p: Parser = Parser() | ||
tree: Expr = p.parse(["!", "(", "A", "&", "!", "B", "|", "C", ")", "<EOF>"]) | ||
counter: OpCounter = OpCounter() | ||
self.assertEqual(counter.getCount(), 0) | ||
|
||
tree.accept(counter) | ||
|
||
self.assertEqual(counter.getCount(), 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import override | ||
|
||
from parser.ast import VarExpr, NotExpr, ParenExpr, AndExpr, OrExpr | ||
from parser.visitor import Visitor | ||
|
||
|
||
class OpCounter(Visitor): | ||
"""Counts the number of boolean operators visited""" | ||
|
||
_count: int = 0 | ||
|
||
@override | ||
def visitNotExpr(self, nex: NotExpr) -> None: | ||
OpCounter._count += 1 | ||
nex.first.accept(self) | ||
|
||
@override | ||
def visitAndExpr(self, aex: AndExpr) -> None: | ||
OpCounter._count += 1 | ||
aex.first.accept(self) | ||
|
||
@override | ||
def visitOrExpr(self, oex: OrExpr) -> None: | ||
OpCounter._count += 1 | ||
oex.first.accept(self) | ||
|
||
def getCount(self) -> int: | ||
return self._count |