Skip to content

Commit

Permalink
haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebBell committed Dec 5, 2024
1 parent 0e80fa3 commit 1f8e151
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dev/haskell-fluids-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for haskell-fluids-test

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
26 changes: 26 additions & 0 deletions dev/haskell-fluids-test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- module Main where
--
-- main :: IO ()
-- main = putStrLn "Hello, Haskell!"
{-# LANGUAGE OverloadedStrings #-}

module Main where

import qualified CPython as Py
import qualified CPython.Simple as PySim

main :: IO ()
main = do
putStrLn "Testing fluids from Haskell..."
-- Initialize Python
Py.initialize

-- Basic test
re <- PySim.call "fluids" "Reynolds"
[PySim.arg (2.5 :: Double), PySim.arg (0.1 :: Double),
PySim.arg (1000.0 :: Double), PySim.arg (0.001 :: Double)] []

putStrLn $ "Reynolds number calculation: " ++ show (re :: Double)

-- Clean up
Py.finalize
75 changes: 75 additions & 0 deletions dev/haskell-fluids-test/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Main where

import qualified CPython as Py
import qualified CPython.Simple as PySim
import Data.Time.Clock.System (getSystemTime, systemSeconds)
import Control.Exception (handle)
import qualified CPython.Types.Exception as PyExc
import Text.Printf (printf)
import Data.Text (Text)

-- Helper function for timing
time :: IO a -> IO (Double, a)
time action = do
start <- getSystemTime
result <- action
end <- getSystemTime
let diff = fromIntegral (systemSeconds end - systemSeconds start)
return (diff, result)

-- Basic fluids test
testFluids :: IO ()
testFluids = do
putStrLn "Testing basic fluids functionality..."

-- Test version
version <- (PySim.getAttribute "fluids" "__version__" :: IO Text)
putStrLn $ "✓ Fluids version: " ++ show version

-- Test Reynolds number
re <- (PySim.call "fluids" "Reynolds"
[PySim.arg (2.5 :: Double), PySim.arg (0.1 :: Double),
PySim.arg (1000.0 :: Double), PySim.arg (0.001 :: Double)] [] :: IO Double)
putStrLn $ "✓ Reynolds number calculation: " ++ show re

-- Test friction factor
fd <- (PySim.call "fluids" "friction_factor"
[PySim.arg (1e5 :: Double), PySim.arg (0.0001 :: Double)] [] :: IO Double)
putStrLn $ "✓ Friction factor calculation: " ++ show fd

putStrLn "Basic tests completed successfully!\n"


-- Benchmark fluids functions
benchmarkFluids :: IO ()
benchmarkFluids = do
putStrLn "Running benchmarks..."

-- Benchmark friction factor
putStrLn "\nBenchmarking friction_factor:"
(t1, _) <- time $ do
sequence $ replicate 1000 $
(PySim.call "fluids" "friction_factor"
[PySim.arg (1e5 :: Double), PySim.arg (0.0001 :: Double)] [] :: IO Double)
putStrLn $ printf "Time for 1000 friction_factor calls: %.6f seconds" t1
putStrLn $ printf "Average time per call: %.6f seconds" (t1 / 1000)

-- Main function to run all tests
main :: IO ()
main = handle pyExceptionHandler $ do
putStrLn "Running fluids tests from Haskell..."
Py.initialize

testFluids
benchmarkFluids

Py.finalize
putStrLn "All tests completed!"
where
pyExceptionHandler :: PyExc.Exception -> IO ()
pyExceptionHandler e = do
putStrLn $ "Python error occurred: " ++ show e
Py.finalize
31 changes: 31 additions & 0 deletions dev/haskell-fluids-test/haskell-fluids-test.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cabal-version: 2.4
name: haskell-fluids-test
version: 0.1.0.0

-- A short (one-line) description of the package.
-- synopsis:

-- A longer description of the package.
-- description:

-- A URL where users can report bugs.
-- bug-reports:

-- The license under which the package is released.
-- license:
author: Caleb Bell
maintainer: Caleb.Andrew.Bell@gmail.com

-- A copyright notice.
-- copyright:
-- category:
extra-source-files: CHANGELOG.md

executable haskell-fluids-test
main-is: Main.hs
build-depends: base ^>=4.15.1.0,
cpython,
time,
text
hs-source-dirs: app
default-language: Haskell2010

0 comments on commit 1f8e151

Please sign in to comment.