Skip to content

Commit

Permalink
Add tests and edit docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklimmm committed May 2, 2024
1 parent ee3ea22 commit 49ed9ad
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/priorityq.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ fn one(val: t, cmp: Cmp(t)) -> PriorityQueue(t) {
/// ```gleam
/// import gleam/int
///
/// new(int.compare) |> is_empty() // -> True
/// from_list([0], int.compare) |> is_empty() // -> False
/// new(int.compare) |> is_empty // -> True
/// from_list([0], int.compare) |> is_empty // -> False
/// ```
///
pub fn is_empty(pq: PriorityQueue(t)) -> Bool {
Expand All @@ -85,7 +85,7 @@ pub fn is_empty(pq: PriorityQueue(t)) -> Bool {
/// ```gleam
/// import gleam/int
///
/// from_list([1, 2, 3], int.compare) |> size() // -> 3
/// from_list([1, 2, 3], int.compare) |> size // -> 3
/// ```
///
pub fn size(pq: PriorityQueue(t)) -> Int {
Expand All @@ -102,8 +102,8 @@ pub fn size(pq: PriorityQueue(t)) -> Int {
/// ```gleam
/// import gleam/int
///
/// new(int.compare) |> peek() // -> None
/// from_list([1, 10, 5], int.compare) |> peek() // -> 10
/// new(int.compare) |> peek // -> None
/// from_list([1, 10, 5], int.compare) |> peek // -> 10
/// ```
///
pub fn peek(pq: PriorityQueue(t)) -> Option(t) {
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn push(pq: PriorityQueue(t), val: t) -> PriorityQueue(t) {
/// ```gleam
/// import gleam/int
///
/// from_list([0]) |> pop() // -> PriorityQueue(Int)
/// from_list([0], int.compare) |> pop // -> PriorityQueue(Int)
/// ```
///
pub fn pop(pq: PriorityQueue(t)) -> PriorityQueue(t) {
Expand Down
61 changes: 57 additions & 4 deletions test/priorityq_test.gleam
Original file line number Diff line number Diff line change
@@ -1,12 +1,65 @@
import gleam/int
import gleam/option.{None, Some}
import gleeunit
import gleeunit/should
import priorityq

pub fn main() {
gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn hello_world_test() {
1
|> should.equal(1)
pub fn peek_test() {
priorityq.from_list([3, 10, 1], int.compare)
|> priorityq.peek
|> should.equal(Some(10))

priorityq.new(int.compare)
|> priorityq.peek
|> should.equal(None)
}

pub fn push_test() {
let pq =
priorityq.from_list([3, 10, 1], int.compare)
|> priorityq.push(15)

pq
|> priorityq.size
|> should.equal(4)
pq
|> priorityq.peek
|> should.equal(Some(15))
}

pub fn pop_test() {
let pq =
priorityq.from_list([3, 10, 1], int.compare)
|> priorityq.pop

pq
|> priorityq.size
|> should.equal(2)
pq
|> priorityq.peek
|> should.equal(Some(3))
}

pub fn is_empty_test() {
priorityq.new(int.compare)
|> priorityq.is_empty
|> should.be_true

priorityq.from_list([3, 1, 2], int.compare)
|> priorityq.is_empty
|> should.be_false
}

pub fn size_test() {
priorityq.new(int.compare)
|> priorityq.size
|> should.equal(0)

priorityq.from_list([3, 1, 2], int.compare)
|> priorityq.size
|> should.equal(3)
}

0 comments on commit 49ed9ad

Please sign in to comment.