Skip to content

Commit 43fcada

Browse files
authored
Hotfix/suffix buffer size (#19)
* Minor robustness fixes * Bumping version + notes * Adding scalar tests and fixed a bug * Adding to the notes
1 parent 4d3c358 commit 43fcada

File tree

12 files changed

+54
-11
lines changed

12 files changed

+54
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [2021-02-08 - Version 1.2.1](https://github.com/matajoh/libnpy/releases/tag/v1.2.1)
4+
5+
Improvements:
6+
- Bug fix for scalar tensor reading
7+
- Bug fix with memstream buffer size at initialization
8+
- ".npy" will be added to tensor names in NPZ writing if not already present
9+
310
## [2021-01-19 - Version 1.2.0](https://github.com/matajoh/libnpy/releases/tag/v1.2.0)
411

512
New Features:

RELEASE_NOTES

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
New Features:
2-
- Easier indexing (variable argument index method + negative indexes)
3-
- Easier access to shape
4-
51
Improvements:
6-
- Cmake upgraded to "modern" usage, i.e. you use the library by adding `npy::npy` as a link library
2+
- Bug fix for scalar tensor reading
3+
- Bug fix with memstream buffer size at initialization
4+
- ".npy" will be added to tensor names in NPZ writing if not already present

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.0
1+
1.2.1

assets/test/int32_scalar.npy

132 Bytes
Binary file not shown.

include/npy/npz.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ class onpzstream
9696

9797
omemstream output;
9898
save(output, tensor);
99-
this->write_file(filename, std::move(output.buf()));
99+
100+
std::string suffix = ".npy";
101+
std::string name = filename;
102+
if(name.size() < 4 || !std::equal(suffix.rbegin(), suffix.rend(), name.rbegin()))
103+
{
104+
name += ".npy";
105+
}
106+
107+
this->write_file(name, std::move(output.buf()));
100108
}
101109

102110
/** Write a tensor to the NPZ archive.

src/memstream.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "npy/core.h"
22

33
namespace {
4-
const int BUFFER_SIZE = 1;//64 * 1024;
4+
const int BUFFER_SIZE = 64 * 1024;
55
}
66

77
namespace npy
@@ -11,8 +11,9 @@ membuf::membuf() : membuf(BUFFER_SIZE)
1111
this->seekpos(0);
1212
}
1313

14-
membuf::membuf(size_t n) : m_buffer(n)
14+
membuf::membuf(size_t n)
1515
{
16+
m_buffer.reserve(BUFFER_SIZE);
1617
this->seekpos(0);
1718
}
1819

src/npy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ void skip_whitespace(std::istream &input)
3737

3838
std::string read_to(std::istream &input, char delim)
3939
{
40+
if(input.peek() == delim)
41+
{
42+
return "";
43+
}
44+
4045
input.get(BUFFER, BUFFER_SIZE, delim);
4146
auto length = input.gcount();
4247
assert(length < BUFFER_SIZE);

test/libnpy_tests.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,17 @@ std::string npy_stream(npy::endian_t endianness = npy::endian_t::NATIVE)
163163
npy::tensor<T> tensor = test_tensor<T>({5, 2, 5});
164164
npy::save(actual_stream, tensor, endianness);
165165
return actual_stream.str();
166-
};
166+
}
167+
168+
template <typename T>
169+
std::string npy_scalar_stream(npy::endian_t endianness = npy::endian_t::NATIVE)
170+
{
171+
std::ostringstream actual_stream;
172+
npy::tensor<T> tensor = test_tensor<T>({});
173+
*tensor.data() = static_cast<T>(42);
174+
npy::save(actual_stream, tensor, endianness);
175+
return actual_stream.str();
176+
}
167177

168178
template <typename T>
169179
std::string npy_fortran_stream(npy::endian_t endianness = npy::endian_t::NATIVE)

test/npy_read.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ int test_npy_read()
1313
test_read<std::uint32_t>(result, "uint32");
1414
test_read<std::int32_t>(result, "int32");
1515
test_read<std::int32_t>(result, "int32_big");
16+
test_read_scalar<std::int32_t>(result, "int32_scalar");
1617
test_read<std::uint64_t>(result, "uint64");
1718
test_read<std::int64_t>(result, "int64");
1819
test_read<float>(result, "float32");

test/npy_read.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ void test_read(int &result, const std::string &name, bool fortran_order = false)
1818
test::assert_equal(expected, actual, result, "npy_read_" + name);
1919
}
2020

21+
template <typename T>
22+
void test_read_scalar(int &result, const std::string &name)
23+
{
24+
npy::tensor<T> expected = test::test_tensor<T>({});
25+
*expected.data() = static_cast<T>(42);
26+
npy::tensor<T> actual = npy::load<T, npy::tensor>(test::asset_path(name + ".npy"));
27+
test::assert_equal(expected, actual, result, "npy_read_" + name);
28+
}
29+
2130
#endif

0 commit comments

Comments
 (0)