Skip to content

Commit

Permalink
[ example ] Print generated sorted binary trees in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buzden committed Nov 11, 2024
1 parent ba2aeb5 commit 7283586
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 0 deletions.
15 changes: 15 additions & 0 deletions examples/sorted-tree-indexed/src/Data/SortedBinTree.idr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Data.SortedBinTree

import public Data.Nat
import Data.String

%default total

Expand All @@ -13,3 +14,17 @@ export
toList : SortedBinTree1 mi ma -> List Nat
toList (Leaf x) = [x]
toList (Node left right) = toList left ++ toList right

export
Interpolation (SortedBinTree1 mi ma) where
interpolate $ Leaf x = "\{show x}"
interpolate $ Node l r = """
.
\{ind "|" $ interpolate l}
\{ind " " $ interpolate r}
"""
where
ind : (pref : String) -> String -> String
ind k s = do
let f::fs = lines s | [] => ""
joinBy "\n" $ "|" :: ("|- " ++ f) :: (("\{k} " ++) <$> fs)
1 change: 1 addition & 0 deletions examples/sorted-tree-indexed/tests/gens/sorted/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ main = do
putStrLn "--------------"
let list = toList tree
putStrLn "min: \{mi}, max: \{ma}, length: \{length list}"
putStrLn "tree:\n\{tree}"
putStrLn "as list: \{show list}"
putStrLn "sorted: \{show $ sorted list}"
96 changes: 96 additions & 0 deletions examples/sorted-tree-indexed/tests/gens/sorted/expected
Original file line number Diff line number Diff line change
@@ -1,40 +1,136 @@
--------------
min: 1, max: 4, length: 2
tree:
.
|
|- 1
|
|- 4
as list: [1, 4]
sorted: True
--------------
min: 0, max: 7, length: 3
tree:
.
|
|- .
| |
| |- 0
| |
| |- 3
|
|- 7
as list: [0, 3, 7]
sorted: True
--------------
min: 3, max: 5, length: 2
tree:
.
|
|- 3
|
|- 5
as list: [3, 5]
sorted: True
--------------
min: 1, max: 7, length: 4
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 7
as list: [1, 2, 3, 7]
sorted: True
--------------
min: 3, max: 6, length: 2
tree:
.
|
|- 3
|
|- 6
as list: [3, 6]
sorted: True
--------------
min: 0, max: 5, length: 5
tree:
.
|
|- .
| |
| |- .
| | |
| | |- .
| | | |
| | | |- 0
| | | |
| | | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 5
as list: [0, 1, 2, 3, 5]
sorted: True
--------------
min: 3, max: 3, length: 1
tree:
3
as list: [3]
sorted: True
--------------
min: 4, max: 4, length: 1
tree:
4
as list: [4]
sorted: True
--------------
min: 0, max: 4, length: 5
tree:
.
|
|- .
| |
| |- .
| | |
| | |- .
| | | |
| | | |- 0
| | | |
| | | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 4
as list: [0, 1, 2, 3, 4]
sorted: True
--------------
min: 1, max: 6, length: 4
tree:
.
|
|- .
| |
| |- .
| | |
| | |- 1
| | |
| | |- 2
| |
| |- 3
|
|- 6
as list: [1, 2, 3, 6]
sorted: True
1 change: 1 addition & 0 deletions examples/sorted-tree-naive/sorted-tree-naive.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sourcedir = "src"
builddir = ".build"

depends = deptycheck
, prettier

modules = Data.SortedBinTree
, Data.SortedBinTree.Gen
16 changes: 16 additions & 0 deletions examples/sorted-tree-naive/src/Data/SortedBinTree.idr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Data.SortedBinTree

import public Data.Nat
import Data.Primitives.Interpolation
import Data.String

%default total

Expand Down Expand Up @@ -35,3 +37,17 @@ export
toList : SortedBinTree -> List Nat
toList Empty = []
toList $ Node x left right = toList left ++ [x] ++ toList right

export
Interpolation SortedBinTree where
interpolate Empty = "*"
interpolate $ Node x l r = """
\{show x}
\{ind "|" $ interpolate l}
\{ind " " $ interpolate r}
"""
where
ind : (pref : String) -> String -> String
ind k s = do
let f::fs = lines s | [] => ""
joinBy "\n" $ "|" :: ("|- " ++ f) :: (("\{k} " ++) <$> fs)
1 change: 1 addition & 0 deletions examples/sorted-tree-naive/tests/gens/sorted/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ main = do
putStrLn "--------------"
let list = toList tree
putStrLn "length: \{length list}"
putStrLn "tree:\n\{tree}"
putStrLn "as list: \{show list}"
putStrLn "sorted: \{show $ sorted list}"
Loading

0 comments on commit 7283586

Please sign in to comment.