Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ jobs:
- name: Setup Carp
uses: carpentry-org/setup-carp@v1

- name: Run tests
run: |
carp -x test/dynlib.carp
carp -x test/typed.carp

- name: Install angler
run: |
set -euo pipefail
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pinned or owned library:
(DynLib.defpinned-binder bind-floor (Fn [Double] Double))

(defn main []
; nb: the math symbols live in different libraries per platform: glibc keeps
; them in libm.so.6, macos in /usr/lib/libSystem.B.dylib
(match (DynLib.open-pinned "libm.so.6")
(Result.Error error) (IO.errorln &error)
(Result.Success library)
Expand Down
11 changes: 6 additions & 5 deletions test/dynlib.carp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
(load "../dynlib.carp")
(load "platform.carp")
(load "Test.carp")
(use Test)

(defn main []
(with-test test
(assert-true test
(Result.success? &(DynLib.open "libm.so.6"))
(Result.success? &(DynLib.open (libm-path)))
"open succeeds for libm")

(assert-true test
(Result.error? &(DynLib.open "libnonexistent.so"))
"open fails for nonexistent library")

(match (DynLib.open "libm.so.6")
(match (DynLib.open (libm-path))
(Result.Success lib)
(let-do [result (the (Result (Fn [Double] Double) String)
(DynLib.get lib "floor"))
Expand All @@ -22,7 +23,7 @@
(assert-true test ok "get succeeds for known symbol"))
(Result.Error _) (assert-true test false "get succeeds for known symbol"))

(match (DynLib.open "libm.so.6")
(match (DynLib.open (libm-path))
(Result.Success lib)
(let-do [result (the (Result (Fn [] ()) String)
(DynLib.get lib "nonexistent_symbol_xyz"))
Expand All @@ -33,14 +34,14 @@
(Result.Error _)
(assert-true test false "get fails for nonexistent symbol"))

(match (DynLib.open "libm.so.6")
(match (DynLib.open (libm-path))
(Result.Success lib)
(assert-true test
(Result.success? &(DynLib.close lib))
"close succeeds on valid handle")
(Result.Error _) (assert-true test false "close succeeds on valid handle"))

(match (DynLib.open "libm.so.6")
(match (DynLib.open (libm-path))
(Result.Success lib)
(assert-equal test
&(Result.Success ())
Expand Down
3 changes: 3 additions & 0 deletions test/platform.carp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; the math symbols live in different libraries per platform: macos keeps them
; in libSystem, glibc in libm.so.6.
(defmacro libm-path [] (if mac-target? "/usr/lib/libSystem.B.dylib" "libm.so.6"))
82 changes: 59 additions & 23 deletions test/typed.carp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(load "../dynlib.carp")
(load "platform.carp")
(load "Test.carp")
(use Test)

(DynLib.defbinder bind-owned-floor (Fn [Double] Double))
(DynLib.defpinned-binder bind-pinned-floor (Fn [Double] Double))
Expand All @@ -7,30 +10,63 @@
(DynLib.defpinned-binder bind-missing (Fn [] ()))

(defn main []
(do
(match (DynLib.open-owned "libm.so.6")
(Result.Error error) (IO.errorln &error)
(with-test test
(match (DynLib.open-owned (libm-path))
(Result.Error error)
(assert-true test false &(fmt "open-owned failed: %s" &error))
(Result.Success library)
(match (bind-owned-floor library "floor")
(Result.Error error) (IO.errorln &error)
(Result.Error error)
(assert-true test false &(fmt "binding floor failed: %s" &error))
(Result.Success function)
(assert (= 3.0 (DynLibBoundFn.call1 &function 3.9)))))
(match (DynLib.open-pinned "libm.so.6")
(Result.Error error) (IO.errorln &error)
(assert-equal test
3.0
(DynLibBoundFn.call1 &function 3.9)
"owned binding calls floor")))

(match (DynLib.open-pinned (libm-path))
(Result.Error error)
(assert-true test false &(fmt "open-pinned failed: %s" &error))
(Result.Success library)
(match (bind-pinned-floor &library "floor")
(Result.Error error)
(assert-true test false &(fmt "binding floor failed: %s" &error))
(Result.Success function)
(assert-equal test
4.0
(DynLibPinnedFn.call1 &function 4.9)
"pinned binding calls floor")))

(match (DynLib.open-pinned (libm-path))
(Result.Error error)
(assert-true test false &(fmt "open-pinned failed: %s" &error))
(Result.Success library)
(match (bind-pinned-ceil &library "ceil")
(Result.Error error)
(assert-true test false &(fmt "binding ceil failed: %s" &error))
(Result.Success function)
(assert-equal test
6.0
(DynLibPinnedFn.call1 &function 5.1)
"pinned binding calls ceil")))

(match (DynLib.open-pinned (libm-path))
(Result.Error error)
(assert-true test false &(fmt "open-pinned failed: %s" &error))
(Result.Success library)
(match (bind-pinned-pow &library "pow")
(Result.Error error)
(assert-true test false &(fmt "binding pow failed: %s" &error))
(Result.Success function)
(assert-equal test
8.0
(DynLibPinnedFn.call2 &function 2.0 3.0)
"pinned binding calls pow")))

(match (DynLib.open-pinned (libm-path))
(Result.Error error)
(assert-true test false &(fmt "open-pinned failed: %s" &error))
(Result.Success library)
(do
(match (bind-pinned-floor &library "floor")
(Result.Error error) (IO.errorln &error)
(Result.Success function)
(assert (= 4.0 (DynLibPinnedFn.call1 &function 4.9))))
(match (bind-pinned-ceil &library "ceil")
(Result.Error error) (IO.errorln &error)
(Result.Success function)
(assert (= 6.0 (DynLibPinnedFn.call1 &function 5.1))))
(match (bind-pinned-pow &library "pow")
(Result.Error error) (IO.errorln &error)
(Result.Success function)
(assert (= 8.0 (DynLibPinnedFn.call2 &function 2.0 3.0))))
(match (bind-missing &library "dynlib_missing_symbol")
(Result.Error _) ()
(Result.Success _) (assert false))))))
(assert-true test
(Result.error? &(bind-missing &library "dynlib_missing_symbol"))
"binding a missing symbol fails"))))