Skip to content

Commit

Permalink
fix to unsigned long Values
Browse files Browse the repository at this point in the history
  • Loading branch information
madronalabs committed Mar 21, 2024
1 parent 17c8fe2 commit ee131df
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 62 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ set(madronalib_VERSION "${madronalib_VERSION_MAJOR}.${madronalib_VERSION_MINOR}"
set(madronalib_VERSION_FULL "${madronalib_VERSION}.${madronalib_VERSION_PATCH}${madronalib_VERSION_EXTRA}")

option(BUILD_EXAMPLES "Build the examples" ON)
option(BUILD_TESTS "Build the ML test programs" ON)
option(BUILD_TESTS "Build the tests" ON)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(ML_BUILD_DOCS "Build the ML documentation" OFF)
option(ML_BUILD_DOCS "Build the documentation" OFF)
option(ML_DOCUMENT_INTERNALS "Include internals in documentation" OFF)

if (ML_BUILD_DOCS)
Expand Down Expand Up @@ -402,8 +402,9 @@ endif()

if(BUILD_TESTS)

# madronalib procs need to be included in projects as source.
# NOTE: madronalib procs need to be included in projects as source.
file(GLOB PROC_SOURCES "source/procs/*.*")

file(GLOB TEST_SOURCES "tests/*.*")

add_executable(tests ${PROC_SOURCES} ${TEST_SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion Tests/queueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace queueTest
{

const size_t kTestSize = 200;
const size_t kTestCount = 500;
const size_t kTestCount = 50;
int transmitSum = 0;
int receiveSum = 0;

Expand Down
6 changes: 4 additions & 2 deletions Tests/timerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using namespace ml;
using namespace std::chrono;

const int longSleepMs{50};

TEST_CASE("madronalib/core/timer/basic", "[timer][basic]")
{
// call this once in an application.
Expand All @@ -43,7 +45,7 @@ TEST_CASE("madronalib/core/timer/basic", "[timer][basic]")
},
milliseconds(10 + 20 * i), 2);
}
std::this_thread::sleep_for(milliseconds(500));
std::this_thread::sleep_for(milliseconds(longSleepMs));
// std::cout << "timer sum: " << sum << "\n";

// not working in GitHub Actions
Expand All @@ -62,7 +64,7 @@ TEST_CASE("madronalib/core/timer/basic", "[timer][basic]")
// in timer::run()
// v2[i]->start([=]() { std::cout << i << " "; }, milliseconds(10 * i));
}
std::this_thread::sleep_for(milliseconds(500));
std::this_thread::sleep_for(milliseconds(longSleepMs));
}

// test stopping timers while running
Expand Down
54 changes: 10 additions & 44 deletions Tests/treeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ TEST_CASE("madronalib/core/serialization", "[serialization]")
}
REQUIRE(!errors);

// Value tree to JSON to value tree.
// Value tree to JSON to value tree. NOTE: the JSON created does not reflect the
// tree structure but rather a flat list with the whole path as each item's string. TODO fix.
Tree< Value > v;
v["a"] = 0.4f;
v["b"] = "hello";
Expand All @@ -395,65 +396,30 @@ TEST_CASE("madronalib/core/serialization", "[serialization]")
auto v3 = JSONToValueTree(textToJSON(t1));
REQUIRE(v == v3);

// text to JSON to value tree
// This time, containing arrays.
TextFragment arraysText = R"({
"file_info": {
"kind": "utu-partial-data",
"version": 1
},
"source": {
"location": "/some/path/on/disk.aiff"
},
"parameters": ["time", "frequency", "amplitude", "bandwidth", "phase"],
"partials": [
{
"label": "component-1",
"parameters": {
"time": [0, 440, 0.3, 0.2, 0],
"frequency": [0.2, 440, 0.3, 0.2, 0],
"amplitude": [0.5, 440, 0.3, 0.2, 0],
"bandwith": [1.5, 440, 0.3, 0.2, 0],
"phase": [3.0, 440, 0.3, 0.2, 0]
}
},
{
"parameters": {
"time": [0.3, 440, 0.3, 0.2, 0],
"frequency": [3.0, 440, 0.3, 0.2, 0],
"amplitude": [4.2, 440, 0.3, 0.2, 0]
}
}
]
})";

auto arraysJSON = textToJSON(arraysText);
auto av2 = JSONToValueTree(arraysJSON);
REQUIRE(av2["file_info/version"] == 1);
}


TEST_CASE("madronalib/core/floatvectors", "[floatvectors]")
{
const int kTestSize{100};

float sum1{0}, sum2{0};

std::vector<float> vec;
for(int i=0; i<kTestSize; ++i)
for (int i = 0; i < kTestSize; ++i)
{
float f = i*13.90811f;
float f = i * 13.90811f;
vec.push_back(f);
sum1 += f;
}

auto vb = floatVectorToBinary(vec);
auto bv = binaryToFloatVector(vb->data());
for(int i=0; i<kTestSize; ++i)

for (int i = 0; i < kTestSize; ++i)
{
sum2 += (*bv)[i];
}

REQUIRE(sum1 == sum2);
}
13 changes: 5 additions & 8 deletions source/app/MLSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ inline TextFragment valueToText(const Value v)
case Value::kUnsignedLongValue:
return TextFragment{"L", textUtils::naturalNumberToText(v.getUnsignedLongValue())};
break;

// TODO blob!
}
return t;
}
Expand Down Expand Up @@ -445,6 +447,8 @@ class JSONHolder
// return a JSON object representing the value tree. The caller is responsible
// for freeing the object.
//
// NOTE: this does not make the JSON tree, rather a flat structure with the
// path name for each object name! TODO fix
inline JSONHolder valueTreeToJSON(const Tree<Value>& t)
{
JSONHolder root;
Expand Down Expand Up @@ -510,14 +514,7 @@ inline Tree<Value> readJSONToValueTree(cJSON* obj, Tree<Value>& r, Path currentP
}
else
{
// it's not clear what to do about arrays. Right now we add a path for each array item to
// mirror the JSON. this has issues:
// - it creates a bunch of symbols for natural numbers (which may not sort as expected!)
// - should numerical symbols even be possible? (no)
// - what we really want from an array, usually, is a blob of float data
// - but we have to inspect all the array elements to know if that is a possible
// representation for the JSON

// TODO add array node markers so we can get back to the JSON original
newObjectPath = Path(currentPath, Path(textUtils::naturalNumberToText(objIndex)));
}

Expand Down
2 changes: 1 addition & 1 deletion source/app/MLTextUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ TextFragment getPath(const TextFragment& frag)
{
return subText(frag, 0, slashLoc);
}
return TextFragment();
return frag;
}

// TODO extend to recognize Cyrillic and other scripts
Expand Down
8 changes: 8 additions & 0 deletions source/app/MLTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@ class Tree
}
}

inline void dumpWithTypes() const
{
for (auto it = begin(); it != end(); ++it)
{
std::cout << it.getCurrentNodePath() << getTypeDebugStr(*it) << " [" << *it << "] \n";
}
}

inline size_t size() const
{
size_t sum{hasValue()}; // me
Expand Down
30 changes: 30 additions & 0 deletions source/app/MLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,36 @@ bool Value::operator!=(const Value& b) const { return !operator==(b); }

#pragma mark Value utilities

std::string getTypeDebugStr(const Value& r) {
std::string out;
switch (r.getType())
{
default:
case Value::kUndefinedValue:
out = "(?)";
break;
case Value::kFloatValue:
out = "(F)";
break;
case Value::kTextValue:
out = "(T)";
break;
case Value::kMatrixValue:
out = "(M)";
break;
case Value::kUnsignedLongValue:
out = "(UL)";
break;
case Value::kBlobValue:
out = "(B)";
break;
case Value::kIntervalValue:
out = "(INT)";
break;
}
return out;
}

std::ostream& operator<<(std::ostream& out, const Value& r)
{
switch (r.getType())
Expand Down
5 changes: 2 additions & 3 deletions source/app/MLValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ struct NamedValue
// Define a type for initializing a new object with a list of Values.
using WithValues = const std::initializer_list<NamedValue>;



std::ostream& operator<<(std::ostream& out, const ml::Value& r);
std::string getTypeDebugStr(const ml::Value& r);
std::ostream& operator<<(std::ostream& out, const ml::Value& r);

} // namespace ml

0 comments on commit ee131df

Please sign in to comment.