Skip to content

Commit

Permalink
Add tests for float_endf
Browse files Browse the repository at this point in the history
  • Loading branch information
paulromano committed Apr 18, 2024
1 parent 24075e2 commit 27a9174
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/endf/_records.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <cstdlib>
#include <cstring> // for strlen

#include <pybind11/pybind11.h>

Expand All @@ -11,7 +12,6 @@
//! only whitespace is to be interpreted as a zero.
//
//! \param buffer character input from an ENDF file
//! \param n Length of character input
//! \return Floating point number

double cfloat_endf(const char* buffer)
Expand All @@ -23,15 +23,13 @@ double cfloat_endf(const char* buffer)

// limit n to 11 characters
int n = std::strlen(buffer);
if (n > 11) n = 11;

int i;

for (i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i) {
char c = buffer[i];

// Skip whitespace characters
if (c == ' ') continue;

if (found_significand) {
if (!found_exponent) {
if (c == '+' || c == '-') {
Expand All @@ -52,7 +50,6 @@ double cfloat_endf(const char* buffer)

// Copy character
arr[j++] = c;

}

// Done copying. Add null terminator and convert to double
Expand Down
53 changes: 53 additions & 0 deletions tests/test_records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# SPDX-FileCopyrightText: Paul Romano
# SPDX-License-Identifier: MIT

from pytest import approx
from endf._records import float_endf


def test_float_sign():
assert float_endf('+3.2146') == approx(3.2146)
assert float_endf('-2.225002+6') == approx(-2.225002e6)


def test_float_no_leading_digit():
assert float_endf('.12345') == approx(0.12345)


def test_float_double_digit_exponent():
assert float_endf('6.022+23') == approx(6.022e23)
assert float_endf('6.022-23') == approx(6.022e-23)


def test_float_whitespace():
assert float_endf(' +1.01+ 2') == approx(101.0)
assert float_endf(' -1.01- 2') == approx(-0.0101)
assert float_endf('+ 2 . 3+ 1') == approx(23.0)
assert float_endf('-7 .8 -1') == approx(-0.78)


def test_float_e_exponent():
assert float_endf('3.14e0') == approx(3.14)
assert float_endf('3.14E0') == approx(3.14)
assert float_endf('3.14e-1') == approx(0.314)


def test_float_d_exponent():
assert float_endf('3.14d0') == approx(3.14)
assert float_endf('3.14D0') == approx(3.14)
assert float_endf('3.14d-1') == approx(0.314)


def test_float_only_leading_digit():
assert float_endf('1+2') == approx(100.0)
assert float_endf('-1+2') == approx(-100.0)
assert float_endf('1.+2') == approx(100.0)
assert float_endf('-1.+2') == approx(-100.0)


def test_float_empty():
assert float_endf(' ') == 0.0


def test_float_buffer_size():
assert float_endf('9.876540000000000') == approx(9.87654)

0 comments on commit 27a9174

Please sign in to comment.