# Good practices for numerical array constants in tests

**URL:** <https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443>\
**Category:** General Usage\
**Tags:** question, testing\
**Created:** [March 28, 2025, 10:03am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443 "2025-03-28T10:03:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 28, 2025, 10:03am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/1 "2025-03-28T10:03:34Z")

</div>

I have isolated a tricky numerical corner case for a package using randomly generated values. I tracked down the problem and fixed it, but want to add it to the tests too so that it does not happen again even if I refactor etc.

What are the best practices for adding array constants, eg a `Float64` matrix, to the tests?

I could just add it on a single line, eg a 20x20 matrix becomes 8k character beast, but otherwise it is fine. Then it is local, but it is still clutter.

I could write to a dlm file and read it back, DelimitedFiles is a lightweight dependency.

Anything else? What would you recommend?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [March 28, 2025, 10:16am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/2 "2025-03-28T10:16:52Z")

</div>

> [@Tamas\_Papp](#):
>
> I could just add it on a single line, eg a 20x20 matrix becomes 8k character beast

Honestly, I would just copy/paste the matrix directly to the file, it is not too big.

You just need to make sure that the decimals match exactly those stored in memory, and are not those rounded by `show` on the REPL.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [March 28, 2025, 10:50am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/3 "2025-03-28T10:50:46Z")

</div>

I would serialize the matrix using the `Serialization` stdlib and keep the file along the test suite. It is a standard library, and it stores the exact values of the floating-point numbers. Moreover, the API is the simplest possible:

```julia
using Serialization
M_ = deserialize("matrix.bin")

```

The format is guaranteed to remain readable between julia versions:

> The data format can change in minor (1.x) Julia releases, but files written by prior 1.x versions will remain readable.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 28, 2025, 11:12am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/4 "2025-03-28T11:12:06Z")

</div>

That gives me an idea… I realized I could just base64 encode the serialized format and have it locally in the code.

> [@Way to "encode" an object?](https://discourse.julialang.org/t/way-to-encode-an-object/26465/4):
>
> If you want a string output, just combine the previously mentioned serialization with base64 encoding. julia\> using Serialization, Base64 julia\> test = base64encode(serialize, (a="Hello World",b=2)) "N0pMBwQAAAA0EAEKTmFtZWRUdXBsZR9OAQRNYWlum0QCAAAAFAJmZxBTH04BBE1haW6bRAIAAAAAIQAIIQtIZWxsbyBXb3JsZOE=" julia\> deserialize(IOBuffer(base64decode(test))) (a = "Hello World", b = 2) Just be aware that the serialization format isn’t designed for long-term storage (i.e, it might change in the future, …

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [March 28, 2025, 11:42am UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/5 "2025-03-28T11:42:11Z")

</div>

![f66](https://global.discourse-cdn.com/julialang/original/3X/f/1/f165fc6ef7ac40b7c09b2bc557644461e2f81f3f.jpeg)  
😄

---

<div class="post-metadata">

**Author:** ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)\
**Post date:** [April 26, 2025, 8:21pm UTC](https://discourse.julialang.org/t/good-practices-for-numerical-array-constants-in-tests/127443/6 "2025-04-26T20:21:41Z")

</div>

Hi Tamas,

If the dataset grows in size, there are a few packages meant to help supply larger data for testing. They include Downloads.jl, RemoteFiles.jl, DataDeps.jl, and Artifacts.jl. I feel like I’m forgetting one. The general idea is that your data has to be somewhere it can be publicly downloaded, and it will be moved into a package-specific area, in most cases. Maybe you could combine it with Scratch.jl, to make a scratch space for testing-only data.  
Hope All Is Well - Drew
