static void test_CarlaUtils() { // ---------------------------------------------------------------------------------------------------------------- // misc functions { bool2str(false); bool2str(true); pass(); char strBuf[2]; nullStrBuf(strBuf); char* const strBuf2(strBuf+1); nullStrBuf(strBuf2); } // ---------------------------------------------------------------------------------------------------------------- // string print functions { carla_debug("DEBUG"); carla_stdout("STDOUT %s", bool2str(true)); carla_stderr("STDERR %s", bool2str(false)); carla_stderr2("STDERR2 " P_UINT64, 0xffffffff); // 4294967295 } // ---------------------------------------------------------------------------------------------------------------- // carla_*sleep { carla_sleep(1); carla_msleep(1); } // ---------------------------------------------------------------------------------------------------------------- // carla_setenv { carla_setenv("THIS", "THAT"); assert(std::strcmp(std::getenv("THIS"), "THAT") == 0); carla_unsetenv("THIS"); assert(std::getenv("THIS") == nullptr); } // ---------------------------------------------------------------------------------------------------------------- // carla_strdup { // with variables const char* const str1(carla_strdup("stringN")); const char* const strF(carla_strdup_free(strdup("stringF"))); const char* const str2(carla_strdup_safe("stringS")); delete[] str1; delete[] str2; delete[] strF; // without variables delete[] carla_strdup("string_normal"); delete[] carla_strdup_free(strdup("string_free")); delete[] carla_strdup_safe("string_safe"); } { // common use case in Carla code struct TestStruct { const char* strNull; const char* strNormal; const char* strFree; const char* strSafe; TestStruct() : strNull(nullptr), strNormal(carla_strdup("strNormal")), strFree(carla_strdup_free(strdup("strFree"))), strSafe(carla_strdup_safe("strSafe")) {} ~TestStruct() noexcept { if (strNull != nullptr) { delete[] strNull; strNull = nullptr; } if (strNormal != nullptr) { delete[] strNormal; strNormal = nullptr; } if (strFree != nullptr) { delete[] strFree; strFree = nullptr; } if (strSafe != nullptr) { delete[] strSafe; strSafe = nullptr; } } CARLA_DECLARE_NON_COPY_STRUCT(TestStruct) }; TestStruct a, b, c; } // ---------------------------------------------------------------------------------------------------------------- // memory functions { int a1[] = { 4, 3, 2, 1 }; int a2[] = { 4, 3, 2, 1 }; int b1[] = { 1, 2, 3, 4 }; int b2[] = { 1, 2, 3, 4 }; carla_add(a1, b1, 4); assert(a1[0] == 5); assert(a1[1] == 5); assert(a1[2] == 5); assert(a1[3] == 5); carla_add(b1, a2, 4); assert(b1[0] == 5); assert(b1[1] == 5); assert(b1[2] == 5); assert(b1[3] == 5); assert(a1[0] == b1[0]); assert(a1[1] == b1[1]); assert(a1[2] == b1[2]); assert(a1[3] == b1[3]); carla_copy(a1, b2, 4); assert(a1[0] != b1[0]); assert(a1[1] != b1[1]); assert(a1[2] != b1[2]); assert(a1[3] != b1[3]); assert(a1[0] == b2[0]); assert(a1[1] == b2[1]); assert(a1[2] == b2[2]); assert(a1[3] == b2[3]); carla_copy(a1, b1, 4); assert(a1[0] == b1[0]); assert(a1[1] == b1[1]); assert(a1[2] == b1[2]); assert(a1[3] == b1[3]); carla_copy(a1, b2, 2); assert(a1[0] != b1[0]); assert(a1[1] != b1[1]); assert(a1[2] == b1[2]); assert(a1[3] == b1[3]); carla_copy(a1+2, b2+2, 2); assert(a1[0] != b1[0]); assert(a1[1] != b1[1]); assert(a1[2] != b1[2]); assert(a1[3] != b1[3]); carla_copy(a1, b1, 2); assert(a1[0] == b1[0]); assert(a1[1] == b1[1]); assert(a1[2] != b1[2]); assert(a1[3] != b1[3]); carla_copy(a1+2, b1+2, 2); assert(a1[0] == b1[0]); assert(a1[1] == b1[1]); assert(a1[2] == b1[2]); assert(a1[3] == b1[3]); carla_fill(a1, 0, 4); assert(a1[0] == 0); assert(a1[1] == 0); assert(a1[2] == 0); assert(a1[3] == 0); carla_fill(a1, -11, 4); assert(a1[0] == -11); assert(a1[1] == -11); assert(a1[2] == -11); assert(a1[3] == -11); carla_fill(a1, 1791, 2); assert(a1[0] == 1791); assert(a1[1] == 1791); assert(a1[2] == -11); assert(a1[3] == -11); carla_fill(a1+2, 1791, 2); assert(a1[0] == 1791); assert(a1[1] == 1791); assert(a1[2] == 1791); assert(a1[3] == 1791); int16_t d = 1527, e = 0; carla_add(&d, &d, 1); assert(d == 1527*2); carla_add(&d, &d, 1); assert(d == 1527*4); carla_add(&d, &e, 1); assert(d == 1527*4); assert(e == 0); carla_add(&e, &d, 1); assert(d == e); carla_add(&e, &d, 1); assert(e == d*2); d = -e; carla_add(&d, &e, 1); assert(d == 0); } { bool x; const bool f = false, t = true; carla_copy(&x, &t, 1); assert(x); carla_copy(&x, &f, 1); assert(! x); carla_fill(&x, true, 1); assert(x); carla_fill(&x, false, 1); assert(! x); } { uint8_t a[] = { 3, 2, 1, 0 }; carla_zeroBytes(a, 1); assert(a[0] == 0); assert(a[1] == 2); assert(a[2] == 1); assert(a[3] == 0); carla_zeroBytes(a+1, 2); assert(a[0] == 0); assert(a[1] == 0); assert(a[2] == 0); assert(a[3] == 0); } { char a[501]; for (int i=500; --i>=0;) a[i] = 'a'; carla_zeroChars(a, 501); for (int i=501; --i>=0;) assert(a[i] == '\0'); for (int i=500; --i>=0;) a[i] = 'a'; assert(std::strlen(a) == 500); carla_fill(a+200, '\0', 1); assert(std::strlen(a) == 200); } { void* a[33]; carla_zeroPointers(a, 33); for (int i=33; --i>=0;) assert(a[i] == nullptr); } { struct Thing { char c; int i; int64_t h; bool operator==(const Thing& t) const noexcept { return (t.c == c && t.i == i && t.h == h); } bool operator!=(const Thing& t) const noexcept { return !operator==(t); } }; Thing a, b, c; a.c = 0; a.i = 0; a.h = 0; b.c = 64; b.i = 64; b.h = 64; c = a; carla_copyStruct(a, b); assert(a == b); carla_zeroStruct(a); assert(a == c); carla_copyStruct(c, b); assert(a != c); // make it non-zero a.c = 1; Thing d[3]; carla_zeroStructs(d, 3); assert(d[0] != a); assert(d[1] != b); assert(d[2] != c); carla_copyStructs(d, &a, 1); assert(d[0] == a); assert(d[1] != b); assert(d[2] != c); carla_copyStructs(&c, d+2, 1); assert(d[0] == a); assert(d[1] != b); assert(d[2] == c); } }
int main() { // ladspa { LADSPA_RDF_ScalePoint scalePoint1, scalePoint2; scalePoint2.Label = carla_strdup(""); LADSPA_RDF_Port port1, port2, port3; port2.Label = carla_strdup(""); port3.Label = carla_strdup(""); port3.ScalePoints = new LADSPA_RDF_ScalePoint[2]; port3.ScalePoints[0].Label = carla_strdup(""); port3.ScalePoints[1].Label = carla_strdup(""); LADSPA_RDF_Descriptor desc1, desc2, desc3; desc2.Title = carla_strdup(""); desc3.Title = carla_strdup(""); desc2.Creator = carla_strdup(""); desc3.Creator = carla_strdup(""); desc3.Ports = new LADSPA_RDF_Port[1]; desc3.Ports[0].Label = carla_strdup(""); desc3.Ports[0].ScalePoints = new LADSPA_RDF_ScalePoint[1]; desc3.Ports[0].ScalePoints[0].Label = carla_strdup(""); } // lv2 { LV2_RDF_PortMidiMap portMidiMap; LV2_RDF_PortPoints portPoints; LV2_RDF_PortUnit portUnit1, portUnit2; portUnit2.Name = carla_strdup(""); portUnit2.Render = carla_strdup(""); portUnit2.Symbol = carla_strdup(""); LV2_RDF_PortScalePoint portScalePoint1, portScalePoint2; portScalePoint2.Label = carla_strdup(""); LV2_RDF_Port port1, port2, port3; port2.Name = carla_strdup(""); port3.Name = carla_strdup(""); port2.Symbol = carla_strdup(""); port3.Symbol = carla_strdup(""); port3.Unit.Name = carla_strdup(""); port3.Unit.Render = carla_strdup(""); port3.Unit.Symbol = carla_strdup(""); port3.ScalePoints = new LV2_RDF_PortScalePoint[2]; port3.ScalePoints[0].Label = carla_strdup(""); port3.ScalePoints[1].Label = carla_strdup(""); LV2_RDF_Preset preset1, preset2; preset2.URI = carla_strdup(""); preset2.Label = carla_strdup(""); LV2_RDF_Feature feat1, feat2; feat2.URI = carla_strdup(""); LV2_RDF_UI ui1, ui2, ui3; ui2.URI = carla_strdup(""); ui3.URI = carla_strdup(""); ui2.Binary = carla_strdup(""); ui3.Binary = carla_strdup(""); ui2.Bundle = carla_strdup(""); ui3.Bundle = carla_strdup(""); ui3.Features = new LV2_RDF_Feature[1]; ui3.Features[0].URI = carla_strdup(""); ui3.Extensions = new LV2_URI[3]; ui3.Extensions[0] = carla_strdup(""); ui3.Extensions[1] = nullptr; ui3.Extensions[2] = carla_strdup(""); ui3.ExtensionCount = 3; LV2_RDF_Descriptor desc1, desc2, desc3; desc2.URI = carla_strdup(""); desc3.URI = carla_strdup(""); desc2.Name = carla_strdup(""); desc3.Name = carla_strdup(""); desc2.Author = carla_strdup(""); desc3.Author = carla_strdup(""); desc2.License = carla_strdup(""); desc3.License = carla_strdup(""); desc2.Binary = carla_strdup(""); desc3.Binary = carla_strdup(""); desc2.Bundle = carla_strdup(""); desc3.Bundle = carla_strdup(""); desc3.Ports = new LV2_RDF_Port[1]; desc3.Ports[0].Name = carla_strdup(""); desc3.Ports[0].Symbol = carla_strdup(""); desc3.Ports[0].Unit.Name = carla_strdup(""); desc3.Ports[0].Unit.Render = carla_strdup(""); desc3.Ports[0].Unit.Symbol = carla_strdup(""); desc3.Ports[0].ScalePoints = new LV2_RDF_PortScalePoint[1]; desc3.Ports[0].ScalePoints[0].Label = carla_strdup(""); desc3.Presets = new LV2_RDF_Preset[1]; desc3.Presets[0].URI = carla_strdup(""); desc3.Presets[0].Label = carla_strdup(""); desc3.Features = new LV2_RDF_Feature[1]; desc3.Features[0].URI = carla_strdup(""); desc3.Extensions = new LV2_URI[3]; desc3.Extensions[0] = carla_strdup(""); desc3.Extensions[1] = nullptr; desc3.Extensions[2] = carla_strdup(""); desc3.ExtensionCount = 3; desc3.UIs = new LV2_RDF_UI[1]; desc3.UIs[0].URI = carla_strdup(""); desc3.UIs[0].Binary = carla_strdup(""); desc3.UIs[0].Bundle = carla_strdup(""); desc3.UIs[0].Features = new LV2_RDF_Feature[1]; desc3.UIs[0].Features[0].URI = carla_strdup(""); desc3.UIs[0].Extensions = new LV2_URI[3]; desc3.UIs[0].Extensions[0] = carla_strdup(""); desc3.UIs[0].Extensions[1] = nullptr; desc3.UIs[0].Extensions[2] = carla_strdup(""); desc3.UIs[0].ExtensionCount = 3; } return 0; }