1
0

test.cpp 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859
  1. /*
  2. * test.cpp
  3. * --------
  4. * Purpose: Unit tests for OpenMPT.
  5. * Notes : We need FAAAAAAAR more unit tests!
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "test.h"
  11. #ifdef ENABLE_TESTS
  12. #include "mpt/base/numbers.hpp"
  13. #include "mpt/crc/crc.hpp"
  14. #include "mpt/environment/environment.hpp"
  15. #include "mpt/io/base.hpp"
  16. #include "mpt/io/io.hpp"
  17. #include "mpt/io/io_stdstream.hpp"
  18. #include "mpt/io_read/filecursor_stdstream.hpp"
  19. #include "mpt/test/test.hpp"
  20. #include "mpt/test/test_macros.hpp"
  21. #include "mpt/uuid/uuid.hpp"
  22. #include "../common/version.h"
  23. #include "../common/misc_util.h"
  24. #include "../common/mptStringBuffer.h"
  25. #include "../common/serialization_utils.h"
  26. #include "../soundlib/Sndfile.h"
  27. #include "../common/FileReader.h"
  28. #include "../soundlib/mod_specifications.h"
  29. #include "../soundlib/MIDIEvents.h"
  30. #include "../soundlib/MIDIMacros.h"
  31. #include "openmpt/soundbase/Copy.hpp"
  32. #include "openmpt/soundbase/SampleConvert.hpp"
  33. #include "openmpt/soundbase/SampleDecode.hpp"
  34. #include "openmpt/soundbase/SampleEncode.hpp"
  35. #include "openmpt/soundbase/SampleFormat.hpp"
  36. #include "../soundlib/SampleCopy.h"
  37. #include "../soundlib/SampleNormalize.h"
  38. #include "../soundlib/ModSampleCopy.h"
  39. #include "../soundlib/ITCompression.h"
  40. #include "../soundlib/tuningcollection.h"
  41. #include "../soundlib/tuning.h"
  42. #include "openmpt/soundbase/Dither.hpp"
  43. #include "../common/Dither.h"
  44. #ifdef MODPLUG_TRACKER
  45. #include "../mptrack/Mptrack.h"
  46. #include "../mptrack/Moddoc.h"
  47. #include "../mptrack/ModDocTemplate.h"
  48. #include "../mptrack/Mainfrm.h"
  49. #include "../mptrack/Settings.h"
  50. #include "../mptrack/HTTP.h"
  51. #endif // MODPLUG_TRACKER
  52. #include "../common/mptFileIO.h"
  53. #ifdef MODPLUG_TRACKER
  54. #include "mpt/crypto/hash.hpp"
  55. #include "mpt/crypto/jwk.hpp"
  56. #include "mpt/uuid_namespace/uuid_namespace.hpp"
  57. #endif // MODPLUG_TRACKER
  58. #ifdef LIBOPENMPT_BUILD
  59. #include "../libopenmpt/libopenmpt_version.h"
  60. #endif // LIBOPENMPT_BUILD
  61. #ifndef NO_PLUGINS
  62. #include "../soundlib/plugins/PlugInterface.h"
  63. #endif
  64. #include <sstream>
  65. #include <limits>
  66. #ifdef LIBOPENMPT_BUILD
  67. #include <iostream>
  68. #endif // LIBOPENMPT_BUILD
  69. #include <istream>
  70. #include <ostream>
  71. #include <stdexcept>
  72. #if MPT_COMPILER_MSVC
  73. #include <tchar.h>
  74. #endif
  75. #if MPT_OS_WINDOWS
  76. #include <windows.h>
  77. #endif
  78. #if defined(MPT_WITH_ZLIB)
  79. #include <zlib.h>
  80. #elif defined(MPT_WITH_MINIZ)
  81. #define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
  82. #include <miniz/miniz.h>
  83. #endif
  84. #define MPT_TEST_HAS_FILESYSTEM 1
  85. #if MPT_OS_DJGPP
  86. #undef MPT_TEST_HAS_FILESYSTEM
  87. #define MPT_TEST_HAS_FILESYSTEM 0
  88. #endif
  89. #include "TestTools.h"
  90. // enable tests which may fail spuriously
  91. //#define FLAKY_TESTS
  92. OPENMPT_NAMESPACE_BEGIN
  93. namespace Test {
  94. static MPT_NOINLINE void TestVersion();
  95. static MPT_NOINLINE void TestTypes();
  96. static MPT_NOINLINE void TestMisc1();
  97. static MPT_NOINLINE void TestMisc2();
  98. static MPT_NOINLINE void TestRandom();
  99. static MPT_NOINLINE void TestCharsets();
  100. static MPT_NOINLINE void TestStringFormatting();
  101. static MPT_NOINLINE void TestSettings();
  102. static MPT_NOINLINE void TestStringIO();
  103. static MPT_NOINLINE void TestMIDIEvents();
  104. static MPT_NOINLINE void TestSampleConversion();
  105. static MPT_NOINLINE void TestITCompression();
  106. static MPT_NOINLINE void TestPCnoteSerialization();
  107. static MPT_NOINLINE void TestLoadSaveFile();
  108. static MPT_NOINLINE void TestEditing();
  109. static mpt::PathString *PathPrefix = nullptr;
  110. static mpt::default_prng * s_PRNG = nullptr;
  111. mpt::PathString GetPathPrefix()
  112. {
  113. if((*PathPrefix).empty())
  114. {
  115. return P_("");
  116. }
  117. return *PathPrefix + P_("/");
  118. }
  119. void DoTests()
  120. {
  121. #ifdef LIBOPENMPT_BUILD
  122. std::cout << std::flush;
  123. std::clog << std::flush;
  124. std::cerr << std::flush;
  125. std::cout << "libopenmpt test suite" << std::endl;
  126. std::cout << "Version.: " << mpt::ToCharset(mpt::Charset::ASCII, Build::GetVersionString(Build::StringVersion | Build::StringRevision | Build::StringSourceInfo | Build::StringBuildFlags | Build::StringBuildFeatures)) << std::endl;
  127. std::cout << "Compiler: " << mpt::ToCharset(mpt::Charset::ASCII, Build::GetBuildCompilerString()) << std::endl;
  128. std::cout << std::flush;
  129. #endif
  130. #if MPT_OS_WINDOWS
  131. // prefix for test suite
  132. std::wstring pathprefix = std::wstring();
  133. bool libopenmpt = false;
  134. #ifdef LIBOPENMPT_BUILD
  135. libopenmpt = true;
  136. #endif
  137. #if !MPT_OS_WINDOWS_WINRT
  138. // set path prefix for test files (if provided)
  139. std::vector<WCHAR> buf(GetEnvironmentVariableW(L"srcdir", NULL, 0) + 1);
  140. if(GetEnvironmentVariableW(L"srcdir", buf.data(), static_cast<DWORD>(buf.size())) > 0)
  141. {
  142. pathprefix = buf.data();
  143. } else
  144. #endif
  145. if(libopenmpt && IsDebuggerPresent())
  146. {
  147. pathprefix = L"../../";
  148. }
  149. PathPrefix = new mpt::PathString(mpt::PathString::FromWide(pathprefix));
  150. #else
  151. // prefix for test suite
  152. mpt::ustring pathprefix = mpt::ustring();
  153. // set path prefix for test files (if provided)
  154. mpt::ustring env_srcdir = mpt::getenv( U_("srcdir") ).value_or( U_("") );
  155. if ( !env_srcdir.empty() ) {
  156. pathprefix = env_srcdir;
  157. }
  158. PathPrefix = new mpt::PathString(mpt::PathString::FromUnicode(pathprefix));
  159. #endif
  160. void (*do_mpt_test)(void) = []() {
  161. mpt_test_reporter reporter{};
  162. mpt::test::run_all(reporter);
  163. };
  164. DO_TEST(do_mpt_test);
  165. mpt::random_device rd;
  166. s_PRNG = new mpt::default_prng(mpt::make_prng<mpt::default_prng>(rd));
  167. DO_TEST(TestVersion);
  168. DO_TEST(TestTypes);
  169. DO_TEST(TestMisc1);
  170. DO_TEST(TestMisc2);
  171. DO_TEST(TestRandom);
  172. DO_TEST(TestCharsets);
  173. DO_TEST(TestStringFormatting);
  174. DO_TEST(TestSettings);
  175. DO_TEST(TestStringIO);
  176. DO_TEST(TestMIDIEvents);
  177. DO_TEST(TestSampleConversion);
  178. DO_TEST(TestITCompression);
  179. // slower tests, require opening a CModDoc
  180. DO_TEST(TestPCnoteSerialization);
  181. DO_TEST(TestLoadSaveFile);
  182. DO_TEST(TestEditing);
  183. delete s_PRNG;
  184. s_PRNG = nullptr;
  185. delete PathPrefix;
  186. PathPrefix = nullptr;
  187. }
  188. static mpt::PathString GetTempFilenameBase();
  189. static void RemoveFile(const mpt::PathString &filename)
  190. {
  191. #if MPT_OS_WINDOWS
  192. for(int retry=0; retry<10; retry++)
  193. {
  194. if(DeleteFile(filename.AsNative().c_str()) != FALSE)
  195. {
  196. break;
  197. }
  198. // wait for windows virus scanners
  199. Sleep(10);
  200. }
  201. #else
  202. remove(filename.AsNative().c_str());
  203. #endif
  204. }
  205. // Test if functions related to program version data work
  206. static MPT_NOINLINE void TestVersion()
  207. {
  208. //Verify that macros and functions work.
  209. {
  210. VERIFY_EQUAL( Version::Parse(Version::Current().ToUString()), Version::Current() );
  211. VERIFY_EQUAL( Version::Parse(Version::Current().ToUString()).ToUString(), Version::Current().ToUString() );
  212. VERIFY_EQUAL( Version(18285096).ToUString(), U_("1.17.02.28") );
  213. VERIFY_EQUAL( Version::Parse(U_("1.17.02.28")), Version(18285096) );
  214. VERIFY_EQUAL( Version::Parse(U_("1.fe.02.28")), Version(0x01fe0228) );
  215. VERIFY_EQUAL( Version::Parse(U_("01.fe.02.28")), Version(0x01fe0228) );
  216. VERIFY_EQUAL( Version::Parse(U_("1.22")), Version(0x01220000) );
  217. VERIFY_EQUAL( MPT_V("1.17.02.28"), Version(18285096) );
  218. VERIFY_EQUAL( MPT_V("1.fe.02.28"), Version(0x01fe0228) );
  219. VERIFY_EQUAL( MPT_V("01.fe.02.28"), Version(0x01fe0228) );
  220. VERIFY_EQUAL( MPT_V("1.22"), Version(0x01220000) );
  221. VERIFY_EQUAL( MPT_V("1.19.02.00").WithoutTestNumber(), MPT_V("1.19.02.00"));
  222. VERIFY_EQUAL( MPT_V("1.18.03.20").WithoutTestNumber(), MPT_V("1.18.03.00"));
  223. VERIFY_EQUAL( MPT_V("1.18.01.13").IsTestVersion(), true);
  224. VERIFY_EQUAL( MPT_V("1.19.01.00").IsTestVersion(), false);
  225. VERIFY_EQUAL( MPT_V("1.17.02.54").IsTestVersion(), false);
  226. VERIFY_EQUAL( MPT_V("1.18.00.00").IsTestVersion(), false);
  227. VERIFY_EQUAL( MPT_V("1.18.02.00").IsTestVersion(), false);
  228. VERIFY_EQUAL( MPT_V("1.18.02.01").IsTestVersion(), true);
  229. // Ensure that versions ending in .00.00 (which are ambiguous to truncated version numbers in certain file formats (e.g. S3M and IT) do not get qualified as test builds.
  230. VERIFY_EQUAL( MPT_V("1.23.00.00").IsTestVersion(), false);
  231. static_assert( MPT_V("1.17.2.28").GetRawVersion() == 18285096 );
  232. static_assert( MPT_V("1.17.02.48").GetRawVersion() == 18285128 );
  233. static_assert( MPT_V("01.17.02.52").GetRawVersion() == 18285138 );
  234. // Ensure that bit-shifting works (used in some mod loaders for example)
  235. static_assert( MPT_V("01.17.00.00").GetRawVersion() == 0x0117 << 16 );
  236. static_assert( MPT_V("01.17.03.00").GetRawVersion() >> 8 == 0x011703 );
  237. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  238. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), false);
  239. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), true);
  240. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05"))), false);
  241. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), true);
  242. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), false);
  243. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  244. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05"))), false);
  245. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  246. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), false);
  247. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  248. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsNewerThan(VersionWithRevision::Parse(U_("1.30.00.05"))), false);
  249. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  250. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), false);
  251. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  252. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsOlderThan(VersionWithRevision::Parse(U_("1.30.00.05"))), false);
  253. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  254. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), true);
  255. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  256. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05"))), false);
  257. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  258. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), true);
  259. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  260. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05-r13099")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05"))), true);
  261. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), false);
  262. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), false);
  263. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), false);
  264. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEqualTo(VersionWithRevision::Parse(U_("1.30.00.05"))), true);
  265. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13100"))), true);
  266. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13099"))), true);
  267. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05-r13098"))), true);
  268. VERIFY_EQUAL(VersionWithRevision::Parse(U_("1.30.00.05")).IsEquivalentTo(VersionWithRevision::Parse(U_("1.30.00.05"))), true);
  269. }
  270. #ifdef MODPLUG_TRACKER
  271. //Verify that the version obtained from the executable file is the same as
  272. //defined in Version.
  273. {
  274. WCHAR szFullPath[MAX_PATH];
  275. DWORD dwVerHnd;
  276. DWORD dwVerInfoSize;
  277. // Get version information from the application
  278. ::GetModuleFileNameW(NULL, szFullPath, mpt::saturate_cast<DWORD>(std::size(szFullPath)));
  279. dwVerInfoSize = ::GetFileVersionInfoSizeW(szFullPath, &dwVerHnd);
  280. if (!dwVerInfoSize)
  281. throw std::runtime_error("!dwVerInfoSize is true");
  282. std::vector<TCHAR> pVersionInfo(dwVerInfoSize);
  283. WCHAR *szVer = nullptr;
  284. UINT uVerLength;
  285. if (!(::GetFileVersionInfoW(szFullPath, (DWORD)dwVerHnd,
  286. (DWORD)dwVerInfoSize, pVersionInfo.data())))
  287. {
  288. throw std::runtime_error("GetFileVersionInfo() returned false");
  289. }
  290. if (!(::VerQueryValueW(pVersionInfo.data(), L"\\StringFileInfo\\040904b0\\FileVersion",
  291. (LPVOID*)&szVer, &uVerLength))) {
  292. throw std::runtime_error("VerQueryValue() returned false");
  293. }
  294. mpt::ustring version = mpt::ToUnicode(szVer);
  295. VERIFY_EQUAL( version, mpt::ufmt::val(Version::Current()) );
  296. VERIFY_EQUAL( Version::Parse(version), Version::Current() );
  297. }
  298. #endif
  299. #ifdef LIBOPENMPT_BUILD
  300. #if MPT_TEST_HAS_FILESYSTEM
  301. #if !MPT_OS_DJGPP
  302. mpt::PathString version_mk = GetPathPrefix() + P_("libopenmpt/libopenmpt_version.mk");
  303. mpt::ifstream f(version_mk, std::ios::in);
  304. VERIFY_EQUAL(f ? true : false, true);
  305. std::map<std::string, std::string> fields;
  306. std::string line;
  307. while(std::getline(f, line))
  308. {
  309. line = mpt::trim(line);
  310. if(line.empty())
  311. {
  312. continue;
  313. }
  314. std::vector<std::string> line_fields = mpt::String::Split<std::string>(line, std::string("="));
  315. VERIFY_EQUAL_NONCONT(line_fields.size(), 2u);
  316. line_fields[0] = mpt::trim(line_fields[0]);
  317. line_fields[1] = mpt::trim(line_fields[1]);
  318. VERIFY_EQUAL_NONCONT(line_fields[0].length() > 0, true);
  319. fields[line_fields[0]] = line_fields[1];
  320. }
  321. VERIFY_EQUAL(fields["LIBOPENMPT_VERSION_MAJOR"], mpt::afmt::val(OPENMPT_API_VERSION_MAJOR));
  322. VERIFY_EQUAL(fields["LIBOPENMPT_VERSION_MINOR"], mpt::afmt::val(OPENMPT_API_VERSION_MINOR));
  323. VERIFY_EQUAL(fields["LIBOPENMPT_VERSION_PATCH"], mpt::afmt::val(OPENMPT_API_VERSION_PATCH));
  324. VERIFY_EQUAL(fields["LIBOPENMPT_VERSION_PREREL"], mpt::afmt::val(OPENMPT_API_VERSION_PREREL));
  325. if(std::string(OPENMPT_API_VERSION_PREREL).length() > 0)
  326. {
  327. VERIFY_EQUAL(std::string(OPENMPT_API_VERSION_PREREL).substr(0, 1), "-");
  328. }
  329. VERIFY_EQUAL(OPENMPT_API_VERSION_IS_PREREL, (std::string(OPENMPT_API_VERSION_PREREL).length() > 0) ? 1 : 0);
  330. VERIFY_EQUAL(fields["LIBOPENMPT_LTVER_CURRENT"].length() > 0, true);
  331. VERIFY_EQUAL(fields["LIBOPENMPT_LTVER_REVISION"].length() > 0, true);
  332. VERIFY_EQUAL(fields["LIBOPENMPT_LTVER_AGE"].length() > 0, true);
  333. #endif // !MPT_OS_DJGPP
  334. #endif // MPT_TEST_HAS_FILESYSTEM
  335. #endif // LIBOPENMPT_BUILD
  336. }
  337. // Test if data types are interpreted correctly
  338. static MPT_NOINLINE void TestTypes()
  339. {
  340. static_assert(sizeof(std::uintptr_t) == sizeof(void*));
  341. #if defined(__SIZEOF_POINTER__)
  342. static_assert(__SIZEOF_POINTER__ == mpt::pointer_size);
  343. static_assert(__SIZEOF_POINTER__ == sizeof(void*));
  344. #endif
  345. VERIFY_EQUAL(int8_min, std::numeric_limits<int8>::min());
  346. VERIFY_EQUAL(int8_max, std::numeric_limits<int8>::max());
  347. VERIFY_EQUAL(uint8_max, std::numeric_limits<uint8>::max());
  348. VERIFY_EQUAL(int16_min, std::numeric_limits<int16>::min());
  349. VERIFY_EQUAL(int16_max, std::numeric_limits<int16>::max());
  350. VERIFY_EQUAL(uint16_max, std::numeric_limits<uint16>::max());
  351. VERIFY_EQUAL(int32_min, std::numeric_limits<int32>::min());
  352. VERIFY_EQUAL(int32_max, std::numeric_limits<int32>::max());
  353. VERIFY_EQUAL(uint32_max, std::numeric_limits<uint32>::max());
  354. VERIFY_EQUAL(int64_min, std::numeric_limits<int64>::min());
  355. VERIFY_EQUAL(int64_max, std::numeric_limits<int64>::max());
  356. VERIFY_EQUAL(uint64_max, std::numeric_limits<uint64>::max());
  357. static_assert(int8_max == std::numeric_limits<int8>::max());
  358. static_assert(uint8_max == std::numeric_limits<uint8>::max());
  359. static_assert(int16_max == std::numeric_limits<int16>::max());
  360. static_assert(uint16_max == std::numeric_limits<uint16>::max());
  361. static_assert(int32_max == std::numeric_limits<int32>::max());
  362. static_assert(uint32_max == std::numeric_limits<uint32>::max());
  363. static_assert(int64_max == std::numeric_limits<int64>::max());
  364. static_assert(uint64_max == std::numeric_limits<uint64>::max());
  365. }
  366. #ifdef MODPLUG_TRACKER
  367. // In tracker debug builds, the sprintf-like function is retained in order to be able to validate our own formatting against sprintf.
  368. // There are 4 reasons why this is not available for library code:
  369. // 1. printf-like functionality is not type-safe.
  370. // 2. There are portability problems with char/wchar_t and the semantics of %s/%ls/%S .
  371. // 3. There are portability problems with specifying format for 64bit integers.
  372. // 4. Formatting of floating point values depends on the currently set C locale.
  373. // A library is not allowed to mock with that and thus cannot influence the behavior in this case.
  374. template <typename T>
  375. static std::string StringFormat(std::string format, T x)
  376. {
  377. #if MPT_COMPILER_MSVC
  378. // Count the needed array size.
  379. const size_t nCount = _scprintf(format.c_str(), x); // null character not included.
  380. std::vector<char> buf(nCount + 1); // + 1 is for null terminator.
  381. sprintf_s(&(buf[0]), buf.size(), format.c_str(), x);
  382. return &(buf[0]);
  383. #else
  384. int size = snprintf(NULL, 0, format.c_str(), x); // get required size, requires c99 compliant snprintf which msvc does not have
  385. std::vector<char> temp(size + 1);
  386. snprintf(&(temp[0]), size + 1, format.c_str(), x);
  387. return &(temp[0]);
  388. #endif
  389. }
  390. #endif
  391. template <typename Tfloat>
  392. static void TestFloatFormat(Tfloat x, std::string format, mpt::FormatFlags f, std::size_t width = 0, int precision = -1)
  393. {
  394. #ifdef MODPLUG_TRACKER
  395. std::string str_sprintf = StringFormat(format, x);
  396. #endif
  397. std::string str_iostreams = mpt::afmt::fmt(x, mpt::FormatSpec().SetFlags(f).SetWidth(width).SetPrecision(precision));
  398. #ifdef MODPLUG_TRACKER
  399. //MPT_LOG_GLOBAL(LogDebug, "test", mpt::ToUnicode(mpt::Charset::ASCII, str_sprintf));
  400. #endif
  401. //MPT_LOG_GLOBAL(LogDebug, "test", mpt::ToUnicode(mpt::Charset::ASCII, str_iostreams));
  402. #ifdef MODPLUG_TRACKER
  403. #if MPT_MSVC_AT_LEAST(2019,4) || MPT_GCC_AT_LEAST(11,1,0)
  404. // to_chars generates shortest form instead of 0-padded precision
  405. if(precision != -1)
  406. #endif
  407. {
  408. VERIFY_EQUAL(str_iostreams, str_sprintf); // this will fail with a set c locale (and there is nothing that can be done about that in libopenmpt)
  409. }
  410. #else
  411. MPT_UNREFERENCED_PARAMETER(format);
  412. MPT_UNUSED_VARIABLE(str_iostreams);
  413. #endif
  414. }
  415. template <typename Tfloat>
  416. static void TestFloatFormats(Tfloat x)
  417. {
  418. TestFloatFormat(x, "%.8g", mpt::afmt::NotaNrm | mpt::afmt::FillOff, 0, 8);
  419. TestFloatFormat(x, MPT_AFORMAT("%.{}g")(std::numeric_limits<Tfloat>::max_digits10), mpt::afmt::NotaNrm | mpt::afmt::FillOff);
  420. TestFloatFormat(x, MPT_AFORMAT("%.{}f")(std::numeric_limits<Tfloat>::digits10), mpt::afmt::NotaFix | mpt::afmt::FillOff);
  421. TestFloatFormat(x, MPT_AFORMAT("%.{}e")(std::numeric_limits<Tfloat>::max_digits10 - 1), mpt::afmt::NotaSci | mpt::afmt::FillOff);
  422. TestFloatFormat(x, "%.0f", mpt::afmt::NotaFix | mpt::afmt::FillOff, 0, 0);
  423. TestFloatFormat(x, "%.1f", mpt::afmt::NotaFix | mpt::afmt::FillOff, 0, 1);
  424. TestFloatFormat(x, "%.2f", mpt::afmt::NotaFix | mpt::afmt::FillOff, 0, 2);
  425. TestFloatFormat(x, "%.3f", mpt::afmt::NotaFix | mpt::afmt::FillOff, 0, 3);
  426. TestFloatFormat(x, "%0.1f", mpt::afmt::NotaFix | mpt::afmt::FillNul, 0, 1);
  427. TestFloatFormat(x, "%02.0f", mpt::afmt::NotaFix | mpt::afmt::FillNul, 2, 0);
  428. }
  429. static MPT_NOINLINE void TestStringFormatting()
  430. {
  431. VERIFY_EQUAL(mpt::afmt::val(1.5f), "1.5");
  432. VERIFY_EQUAL(mpt::afmt::val(true), "1");
  433. VERIFY_EQUAL(mpt::afmt::val(false), "0");
  434. //VERIFY_EQUAL(mpt::afmt::val('A'), "A"); // deprecated
  435. //VERIFY_EQUAL(mpt::afmt::val(L'A'), "A"); // deprecated
  436. VERIFY_EQUAL(mpt::afmt::val(0), "0");
  437. VERIFY_EQUAL(mpt::afmt::val(-23), "-23");
  438. VERIFY_EQUAL(mpt::afmt::val(42), "42");
  439. VERIFY_EQUAL(mpt::afmt::hex0<3>((int32)-1), "-001");
  440. VERIFY_EQUAL(mpt::afmt::hex((int32)-1), "-1");
  441. VERIFY_EQUAL(mpt::afmt::hex(-0xabcde), "-abcde");
  442. VERIFY_EQUAL(mpt::afmt::hex(int32_min), "-80000000");
  443. VERIFY_EQUAL(mpt::afmt::hex(int32_min + 1), "-7fffffff");
  444. VERIFY_EQUAL(mpt::afmt::hex(0x123e), "123e");
  445. VERIFY_EQUAL(mpt::afmt::hex0<6>(0x123e), "00123e");
  446. VERIFY_EQUAL(mpt::afmt::hex0<2>(0x123e), "123e");
  447. VERIFY_EQUAL(mpt::afmt::dec0<0>(1), "1");
  448. VERIFY_EQUAL(mpt::afmt::dec0<1>(1), "1");
  449. VERIFY_EQUAL(mpt::afmt::dec0<2>(1), "01");
  450. VERIFY_EQUAL(mpt::afmt::dec0<3>(1), "001");
  451. VERIFY_EQUAL(mpt::afmt::dec0<0>(11), "11");
  452. VERIFY_EQUAL(mpt::afmt::dec0<1>(11), "11");
  453. VERIFY_EQUAL(mpt::afmt::dec0<2>(11), "11");
  454. VERIFY_EQUAL(mpt::afmt::dec0<3>(11), "011");
  455. VERIFY_EQUAL(mpt::afmt::dec0<0>(-1), "-1");
  456. VERIFY_EQUAL(mpt::afmt::dec0<1>(-1), "-1");
  457. VERIFY_EQUAL(mpt::afmt::dec0<2>(-1), "-01");
  458. VERIFY_EQUAL(mpt::afmt::dec0<3>(-1), "-001");
  459. VERIFY_EQUAL(mpt::ufmt::HEX0<7>(0xa2345678), U_("A2345678"));
  460. VERIFY_EQUAL(mpt::ufmt::HEX0<8>(0xa2345678), U_("A2345678"));
  461. VERIFY_EQUAL(mpt::ufmt::HEX0<9>(0xa2345678), U_("0A2345678"));
  462. VERIFY_EQUAL(mpt::ufmt::HEX0<10>(0xa2345678), U_("00A2345678"));
  463. #if MPT_WSTRING_FORMAT
  464. VERIFY_EQUAL(mpt::wfmt::hex(0x123e), L"123e");
  465. VERIFY_EQUAL(mpt::wfmt::hex0<6>(0x123e), L"00123e");
  466. VERIFY_EQUAL(mpt::wfmt::hex0<2>(0x123e), L"123e");
  467. #endif
  468. VERIFY_EQUAL(mpt::afmt::val(-87.0f), "-87");
  469. if(mpt::afmt::val(-0.5e-6) != "-5e-007"
  470. && mpt::afmt::val(-0.5e-6) != "-5e-07"
  471. && mpt::afmt::val(-0.5e-6) != "-5e-7"
  472. && mpt::afmt::val(-0.5e-6) != "-4.9999999999999998e-7"
  473. && mpt::afmt::val(-0.5e-6) != "-4.9999999999999998e-07"
  474. && mpt::afmt::val(-0.5e-6) != "-4.9999999999999998e-007"
  475. )
  476. {
  477. VERIFY_EQUAL(true, false);
  478. }
  479. if(mpt::afmt::val(-1.0 / 65536.0) != "-1.52587890625e-005"
  480. && mpt::afmt::val(-1.0 / 65536.0) != "-1.52587890625e-05"
  481. && mpt::afmt::val(-1.0 / 65536.0) != "-1.52587890625e-5"
  482. )
  483. {
  484. VERIFY_EQUAL(true, false);
  485. }
  486. if(mpt::afmt::val(-1.0f / 65536.0f) != "-1.52587891e-005"
  487. && mpt::afmt::val(-1.0f / 65536.0f) != "-1.52587891e-05"
  488. && mpt::afmt::val(-1.0f / 65536.0f) != "-1.52587891e-5"
  489. && mpt::afmt::val(-1.0f / 65536.0f) != "-1.5258789e-005"
  490. && mpt::afmt::val(-1.0f / 65536.0f) != "-1.5258789e-05"
  491. && mpt::afmt::val(-1.0f / 65536.0f) != "-1.5258789e-5"
  492. )
  493. {
  494. VERIFY_EQUAL(true, false);
  495. }
  496. if(mpt::afmt::val(58.65403492763) != "58.654034927630001"
  497. && mpt::afmt::val(58.65403492763) != "58.65403492763"
  498. )
  499. {
  500. VERIFY_EQUAL(true, false);
  501. }
  502. VERIFY_EQUAL(mpt::afmt::flt(58.65403492763, 6), "58.654");
  503. VERIFY_EQUAL(mpt::afmt::fix(23.42, 1), "23.4");
  504. VERIFY_EQUAL(mpt::afmt::fix(234.2, 1), "234.2");
  505. VERIFY_EQUAL(mpt::afmt::fix(2342.0, 1), "2342.0");
  506. VERIFY_EQUAL(mpt::afmt::dec(2, ';', 2345678), std::string("2;34;56;78"));
  507. VERIFY_EQUAL(mpt::afmt::dec(2, ';', 12345678), std::string("12;34;56;78"));
  508. VERIFY_EQUAL(mpt::afmt::hex(3, ':', 0xa2345678), std::string("a2:345:678"));
  509. VERIFY_EQUAL(mpt::ufmt::dec(2, ';', 12345678), U_("12;34;56;78"));
  510. VERIFY_EQUAL(mpt::ufmt::hex(3, ':', 0xa2345678), U_("a2:345:678"));
  511. VERIFY_EQUAL(mpt::ufmt::HEX0<7>(3, ':', 0xa2345678), U_("A2:345:678"));
  512. VERIFY_EQUAL(mpt::ufmt::HEX0<8>(3, ':', 0xa2345678), U_("A2:345:678"));
  513. VERIFY_EQUAL(mpt::ufmt::HEX0<9>(3, ':', 0xa2345678), U_("0A2:345:678"));
  514. VERIFY_EQUAL(mpt::ufmt::HEX0<10>(3, ':', 0xa2345678), U_("0:0A2:345:678"));
  515. VERIFY_EQUAL(mpt::ufmt::HEX0<11>(3, ':', 0xa2345678), U_("00:0A2:345:678"));
  516. VERIFY_EQUAL(mpt::ufmt::HEX0<12>(3, ':', 0xa2345678), U_("000:0A2:345:678"));
  517. VERIFY_EQUAL(mpt::ufmt::HEX0<7>(3, ':', -0x12345678), U_("-12:345:678"));
  518. VERIFY_EQUAL(mpt::ufmt::HEX0<8>(3, ':', -0x12345678), U_("-12:345:678"));
  519. VERIFY_EQUAL(mpt::ufmt::HEX0<9>(3, ':', -0x12345678), U_("-012:345:678"));
  520. VERIFY_EQUAL(mpt::ufmt::HEX0<10>(3, ':', -0x12345678), U_("-0:012:345:678"));
  521. VERIFY_EQUAL(mpt::ufmt::HEX0<11>(3, ':', -0x12345678), U_("-00:012:345:678"));
  522. VERIFY_EQUAL(mpt::ufmt::HEX0<12>(3, ':', -0x12345678), U_("-000:012:345:678"));
  523. VERIFY_EQUAL(mpt::ufmt::HEX0<5>(3, ':', 0x345678), U_("345:678"));
  524. VERIFY_EQUAL(mpt::ufmt::HEX0<6>(3, ':', 0x345678), U_("345:678"));
  525. VERIFY_EQUAL(mpt::ufmt::HEX0<7>(3, ':', 0x345678), U_("0:345:678"));
  526. VERIFY_EQUAL(mpt::ufmt::HEX0<5>(3, ':', -0x345678), U_("-345:678"));
  527. VERIFY_EQUAL(mpt::ufmt::HEX0<6>(3, ':', -0x345678), U_("-345:678"));
  528. VERIFY_EQUAL(mpt::ufmt::HEX0<7>(3, ':', -0x345678), U_("-0:345:678"));
  529. VERIFY_EQUAL(mpt::afmt::left(3, "a"), "a ");
  530. VERIFY_EQUAL(mpt::afmt::right(3, "a"), " a");
  531. VERIFY_EQUAL(mpt::afmt::center(3, "a"), " a ");
  532. VERIFY_EQUAL(mpt::afmt::center(4, "a"), " a ");
  533. #if defined(MPT_WITH_MFC)
  534. VERIFY_EQUAL(mpt::cfmt::left(3, CString(_T("a"))), CString(_T("a ")));
  535. VERIFY_EQUAL(mpt::cfmt::right(3, CString(_T("a"))), CString(_T(" a")));
  536. VERIFY_EQUAL(mpt::cfmt::center(3, CString(_T("a"))), CString(_T(" a ")));
  537. VERIFY_EQUAL(mpt::cfmt::center(4, CString(_T("a"))), CString(_T(" a ")));
  538. #endif // MPT_WITH_MFC
  539. VERIFY_EQUAL(ConvertStrTo<uint32>("586"), 586u);
  540. VERIFY_EQUAL(ConvertStrTo<uint32>("2147483647"), (uint32)int32_max);
  541. VERIFY_EQUAL(ConvertStrTo<uint32>("4294967295"), uint32_max);
  542. VERIFY_EQUAL(ConvertStrTo<int64>("-9223372036854775808"), int64_min);
  543. VERIFY_EQUAL(ConvertStrTo<int64>("-159"), -159);
  544. VERIFY_EQUAL(ConvertStrTo<int64>("9223372036854775807"), int64_max);
  545. VERIFY_EQUAL(ConvertStrTo<uint64>("85059"), 85059u);
  546. VERIFY_EQUAL(ConvertStrTo<uint64>("9223372036854775807"), (uint64)int64_max);
  547. VERIFY_EQUAL(ConvertStrTo<uint64>("18446744073709551615"), uint64_max);
  548. VERIFY_EQUAL(ConvertStrTo<float>("-87.0"), -87.0f);
  549. #if !MPT_OS_DJGPP
  550. VERIFY_EQUAL(ConvertStrTo<double>("-0.5e-6"), -0.5e-6);
  551. #endif
  552. #if !MPT_OS_DJGPP
  553. VERIFY_EQUAL(ConvertStrTo<double>("58.65403492763"), 58.65403492763);
  554. #else
  555. VERIFY_EQUAL_EPS(ConvertStrTo<double>("58.65403492763"), 58.65403492763, 0.0001);
  556. #endif
  557. VERIFY_EQUAL(ConvertStrTo<float>(mpt::afmt::val(-87.0)), -87.0f);
  558. #if !MPT_OS_DJGPP
  559. VERIFY_EQUAL(ConvertStrTo<double>(mpt::afmt::val(-0.5e-6)), -0.5e-6);
  560. #endif
  561. VERIFY_EQUAL(mpt::String::Parse::Hex<unsigned char>("fe"), 254);
  562. #if MPT_WSTRING_FORMAT
  563. VERIFY_EQUAL(mpt::String::Parse::Hex<unsigned char>(L"fe"), 254);
  564. #endif
  565. VERIFY_EQUAL(mpt::String::Parse::Hex<unsigned int>(U_("ffff")), 65535);
  566. TestFloatFormats(0.0f);
  567. TestFloatFormats(-0.0f);
  568. TestFloatFormats(1.0f);
  569. TestFloatFormats(-1.0f);
  570. TestFloatFormats(0.1f);
  571. TestFloatFormats(-0.1f);
  572. TestFloatFormats(1000000000.0f);
  573. TestFloatFormats(-1000000000.0f);
  574. TestFloatFormats(0.0000000001f);
  575. TestFloatFormats(-0.0000000001f);
  576. TestFloatFormats(6.12345f);
  577. TestFloatFormats(0.0);
  578. TestFloatFormats(-0.0);
  579. TestFloatFormats(1.0);
  580. TestFloatFormats(-1.0);
  581. TestFloatFormats(0.1);
  582. TestFloatFormats(-0.1);
  583. TestFloatFormats(1000000000.0);
  584. TestFloatFormats(-1000000000.0);
  585. TestFloatFormats(0.0000000001);
  586. TestFloatFormats(-0.0000000001);
  587. TestFloatFormats(6.12345);
  588. TestFloatFormats(42.1234567890);
  589. TestFloatFormats(0.1234567890);
  590. TestFloatFormats(1234567890000000.0);
  591. TestFloatFormats(0.0000001234567890);
  592. TestFloatFormats(mpt::numbers::pi);
  593. TestFloatFormats(3.14159265358979323846);
  594. TestFloatFormats(3.14159265358979323846f);
  595. VERIFY_EQUAL(mpt::afmt::flt(6.12345, 3), "6.12");
  596. VERIFY_EQUAL(mpt::afmt::fix(6.12345, 3), "6.123");
  597. VERIFY_EQUAL(mpt::afmt::flt(6.12345, 4), "6.123");
  598. VERIFY_EQUAL(mpt::afmt::fix(6.12345, 4), "6.1235");
  599. #if MPT_WSTRING_FORMAT
  600. VERIFY_EQUAL(mpt::wfmt::flt(6.12345, 3), L"6.12");
  601. VERIFY_EQUAL(mpt::wfmt::fix(6.12345, 3), L"6.123");
  602. VERIFY_EQUAL(mpt::wfmt::flt(6.12345, 4), L"6.123");
  603. #endif
  604. static_assert(mpt::parse_format_string_argument_count("") == 0);
  605. static_assert(mpt::parse_format_string_argument_count("{{") == 0);
  606. static_assert(mpt::parse_format_string_argument_count("}}") == 0);
  607. static_assert(mpt::parse_format_string_argument_count("{}") == 1);
  608. static_assert(mpt::parse_format_string_argument_count("{}{}") == 2);
  609. static_assert(mpt::parse_format_string_argument_count("{0}{1}") == 2);
  610. // basic
  611. VERIFY_EQUAL(MPT_AFORMAT("{}{}{}")(1,2,3), "123");
  612. VERIFY_EQUAL(MPT_AFORMAT("{2}{1}{0}")(1,2,3), "321");
  613. VERIFY_EQUAL(MPT_AFORMAT("{2}{1}{0}{4}{3}{6}{5}{7}{10}{9}{8}")(0,1,2,3,4,5,6,7,8,9,"a"), "21043657a98");
  614. //VERIFY_EQUAL(MPT_AFORMAT("{2}{1}{0}{2}{1}{0}{10}{9}{8}")(0,1,2,3,4,5,6,7,8,9,"a"), "210210a98");
  615. #if MPT_WSTRING_FORMAT
  616. VERIFY_EQUAL(MPT_WFORMAT("{}{}{}")(1,2,3), L"123");
  617. #endif
  618. // escaping behviour
  619. VERIFY_EQUAL(MPT_AFORMAT("%")(), "%");
  620. VERIFY_EQUAL(MPT_AFORMAT("%")(), "%");
  621. VERIFY_EQUAL(MPT_AFORMAT("%%")(), "%%");
  622. VERIFY_EQUAL(MPT_AFORMAT("{}")("a"), "a");
  623. VERIFY_EQUAL(MPT_AFORMAT("{}%")("a"), "a%");
  624. VERIFY_EQUAL(MPT_AFORMAT("{}%")("a"), "a%");
  625. VERIFY_EQUAL(MPT_AFORMAT("{}%%")("a"), "a%%");
  626. VERIFY_EQUAL(MPT_AFORMAT("%1")(), "%1");
  627. VERIFY_EQUAL(MPT_AFORMAT("%{}")("a"), "%a");
  628. VERIFY_EQUAL(MPT_AFORMAT("%b")(), "%b");
  629. VERIFY_EQUAL(MPT_AFORMAT("{{}}")(), "{}");
  630. VERIFY_EQUAL(MPT_AFORMAT("{{{}}}")("a"), "{a}");
  631. #if defined(MPT_WITH_MFC)
  632. VERIFY_EQUAL(mpt::ufmt::val(CString(_T("foobar"))), U_("foobar"));
  633. VERIFY_EQUAL(mpt::ufmt::val(CString(_T("foobar"))), U_("foobar"));
  634. VERIFY_EQUAL(MPT_CFORMAT("{}{}{}")(1,2,3), _T("123"));
  635. VERIFY_EQUAL(MPT_CFORMAT("{}{}{}")(1,mpt::cfmt::dec0<3>(2),3), _T("10023"));
  636. #endif // MPT_WITH_MFC
  637. FlagSet<MODTYPE> foo;
  638. foo.set(MOD_TYPE_MOD, true);
  639. VERIFY_EQUAL(MPT_UFORMAT("{}")(foo), U_("00000000000000000000000000000001"));
  640. }
  641. namespace {
  642. struct Gregorian {
  643. int Y,M,D,h,m,s;
  644. static Gregorian FromTM(tm t) {
  645. Gregorian g;
  646. g.Y = t.tm_year + 1900;
  647. g.M = t.tm_mon + 1;
  648. g.D = t.tm_mday;
  649. g.h = t.tm_hour;
  650. g.m = t.tm_min;
  651. g.s = t.tm_sec;
  652. return g;
  653. }
  654. static tm ToTM(Gregorian g) {
  655. tm t;
  656. MemsetZero(t);
  657. t.tm_year = g.Y - 1900;
  658. t.tm_mon = g.M - 1;
  659. t.tm_mday = g.D;
  660. t.tm_hour = g.h;
  661. t.tm_min = g.m;
  662. t.tm_sec = g.s;
  663. return t;
  664. }
  665. };
  666. inline bool operator ==(Gregorian a, Gregorian b) {
  667. return a.Y == b.Y && a.M == b.M && a.D == b.D && a.h == b.h && a.m == b.m && a.s == b.s;
  668. }
  669. }
  670. static int64 TestDate1(int s, int m, int h, int D, int M, int Y) {
  671. return mpt::Date::Unix::FromUTC(Gregorian::ToTM(Gregorian{Y,M,D,h,m,s}));
  672. }
  673. static Gregorian TestDate2(int s, int m, int h, int D, int M, int Y) {
  674. return Gregorian{Y,M,D,h,m,s};
  675. }
  676. static MPT_NOINLINE void TestMisc1()
  677. {
  678. VERIFY_EQUAL(ModCommand::IsPcNote(NOTE_MAX), false);
  679. VERIFY_EQUAL(ModCommand::IsPcNote(NOTE_PC), true);
  680. VERIFY_EQUAL(ModCommand::IsPcNote(NOTE_PCS), true);
  681. VERIFY_EQUAL(CModSpecifications::ExtensionToType(".mod"), MOD_TYPE_MOD);
  682. VERIFY_EQUAL(CModSpecifications::ExtensionToType("mod"), MOD_TYPE_MOD);
  683. VERIFY_EQUAL(CModSpecifications::ExtensionToType(".s3m"), MOD_TYPE_S3M);
  684. VERIFY_EQUAL(CModSpecifications::ExtensionToType("s3m"), MOD_TYPE_S3M);
  685. VERIFY_EQUAL(CModSpecifications::ExtensionToType(".xm"), MOD_TYPE_XM);
  686. VERIFY_EQUAL(CModSpecifications::ExtensionToType("xm"), MOD_TYPE_XM);
  687. VERIFY_EQUAL(CModSpecifications::ExtensionToType(".it"), MOD_TYPE_IT);
  688. VERIFY_EQUAL(CModSpecifications::ExtensionToType("it"), MOD_TYPE_IT);
  689. VERIFY_EQUAL(CModSpecifications::ExtensionToType(".itp"), MOD_TYPE_NONE);
  690. VERIFY_EQUAL(CModSpecifications::ExtensionToType("itp"), MOD_TYPE_NONE);
  691. VERIFY_EQUAL(CModSpecifications::ExtensionToType("mptm"), MOD_TYPE_MPT);
  692. VERIFY_EQUAL(CModSpecifications::ExtensionToType("invalidExtension"), MOD_TYPE_NONE);
  693. VERIFY_EQUAL(CModSpecifications::ExtensionToType("ita"), MOD_TYPE_NONE);
  694. VERIFY_EQUAL(CModSpecifications::ExtensionToType("s2m"), MOD_TYPE_NONE);
  695. VERIFY_EQUAL(CModSpecifications::ExtensionToType(""), MOD_TYPE_NONE);
  696. // invalid
  697. VERIFY_EQUAL(SampleFormat::FromInt(0), SampleFormat::Invalid);
  698. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0000'11'0), SampleFormat::Invalid);
  699. // correct
  700. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0001'00'1), SampleFormat::Unsigned8);
  701. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0001'00'0), SampleFormat::Int8);
  702. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0010'00'0), SampleFormat::Int16);
  703. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0011'00'0), SampleFormat::Int24);
  704. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0100'00'0), SampleFormat::Int32);
  705. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0100'00'0), SampleFormat::Float32);
  706. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1000'00'0), SampleFormat::Float64);
  707. // no size
  708. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0000'00'0), SampleFormat::Invalid);
  709. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0000'00'1), SampleFormat::Unsigned8);
  710. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0000'00'0), SampleFormat::Float32);
  711. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0000'00'1), SampleFormat::Invalid);
  712. // invalid unsigned
  713. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0010'00'1), SampleFormat::Int16);
  714. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0011'00'1), SampleFormat::Int24);
  715. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0100'00'1), SampleFormat::Int32);
  716. // invalid float
  717. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0001'00'0), SampleFormat::Int8);
  718. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0010'00'0), SampleFormat::Int16);
  719. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0011'00'0), SampleFormat::Int24);
  720. // bogus size
  721. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0001'00'0), SampleFormat::Int8);
  722. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0010'00'0), SampleFormat::Int16);
  723. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0011'00'0), SampleFormat::Int24);
  724. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0100'00'0), SampleFormat::Int32);
  725. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0101'00'0), SampleFormat::Int32);
  726. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0110'00'0), SampleFormat::Int16);
  727. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0111'00'0), SampleFormat::Int24);
  728. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1000'00'0), SampleFormat::Float64);
  729. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1001'00'0), SampleFormat::Float64);
  730. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1010'00'0), SampleFormat::Int16);
  731. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1011'00'0), SampleFormat::Int24);
  732. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1100'00'0), SampleFormat::Int32);
  733. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1101'00'0), SampleFormat::Int32);
  734. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1110'00'0), SampleFormat::Int16);
  735. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1111'00'0), SampleFormat::Int24);
  736. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0001'00'1), SampleFormat::Unsigned8);
  737. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0010'00'1), SampleFormat::Int16);
  738. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0011'00'1), SampleFormat::Int24);
  739. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0100'00'1), SampleFormat::Int32);
  740. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0101'00'1), SampleFormat::Int32);
  741. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0110'00'1), SampleFormat::Int16);
  742. VERIFY_EQUAL(SampleFormat::FromInt(0b0'0111'00'1), SampleFormat::Int24);
  743. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1000'00'1), SampleFormat::Float64);
  744. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1001'00'1), SampleFormat::Float64);
  745. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1010'00'1), SampleFormat::Int16);
  746. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1011'00'1), SampleFormat::Int24);
  747. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1100'00'1), SampleFormat::Int32);
  748. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1101'00'1), SampleFormat::Int32);
  749. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1110'00'1), SampleFormat::Int16);
  750. VERIFY_EQUAL(SampleFormat::FromInt(0b0'1111'00'1), SampleFormat::Int24);
  751. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0001'00'0), SampleFormat::Int8);
  752. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0010'00'0), SampleFormat::Int16);
  753. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0011'00'0), SampleFormat::Int24);
  754. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0100'00'0), SampleFormat::Float32);
  755. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0101'00'0), SampleFormat::Float32);
  756. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0110'00'0), SampleFormat::Int16);
  757. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0111'00'0), SampleFormat::Int24);
  758. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1000'00'0), SampleFormat::Float64);
  759. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1001'00'0), SampleFormat::Float64);
  760. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1010'00'0), SampleFormat::Int16);
  761. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1011'00'0), SampleFormat::Int24);
  762. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1100'00'0), SampleFormat::Float32);
  763. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1101'00'0), SampleFormat::Float32);
  764. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1110'00'0), SampleFormat::Int16);
  765. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1111'00'0), SampleFormat::Int24);
  766. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0001'00'1), SampleFormat::Unsigned8);
  767. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0010'00'1), SampleFormat::Int16);
  768. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0011'00'1), SampleFormat::Int24);
  769. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0100'00'1), SampleFormat::Float32);
  770. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0101'00'1), SampleFormat::Float32);
  771. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0110'00'1), SampleFormat::Int16);
  772. VERIFY_EQUAL(SampleFormat::FromInt(0b1'0111'00'1), SampleFormat::Int24);
  773. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1000'00'1), SampleFormat::Float64);
  774. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1001'00'1), SampleFormat::Float64);
  775. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1010'00'1), SampleFormat::Int16);
  776. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1011'00'1), SampleFormat::Int24);
  777. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1100'00'1), SampleFormat::Float32);
  778. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1101'00'1), SampleFormat::Float32);
  779. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1110'00'1), SampleFormat::Int16);
  780. VERIFY_EQUAL(SampleFormat::FromInt(0b1'1111'00'1), SampleFormat::Int24);
  781. }
  782. static MPT_NOINLINE void TestMisc2()
  783. {
  784. // Check for completeness of supported effect list in mod specifications
  785. for(const auto &spec : ModSpecs::Collection)
  786. {
  787. VERIFY_EQUAL(strlen(spec->commands), (size_t)MAX_EFFECTS);
  788. VERIFY_EQUAL(strlen(spec->volcommands), (size_t)MAX_VOLCMDS);
  789. }
  790. #ifdef MODPLUG_TRACKER
  791. #ifdef MPT_ENABLE_FILEIO
  792. {
  793. std::vector<std::byte> data;
  794. data.push_back(mpt::as_byte(0));
  795. data.push_back(mpt::as_byte(255));
  796. data.push_back(mpt::as_byte(1));
  797. data.push_back(mpt::as_byte(2));
  798. mpt::PathString fn = GetTempFilenameBase() + P_("lazy");
  799. RemoveFile(fn);
  800. mpt::LazyFileRef f(fn);
  801. f = data;
  802. std::vector<std::byte> data2;
  803. data2 = f;
  804. VERIFY_EQUAL(data.size(), data2.size());
  805. for(std::size_t i = 0; i < data.size() && i < data2.size(); ++i)
  806. {
  807. VERIFY_EQUAL(data[i], data2[i]);
  808. }
  809. RemoveFile(fn);
  810. }
  811. #endif
  812. #endif // MODPLUG_TRACKER
  813. #ifdef MPT_WITH_ZLIB
  814. VERIFY_EQUAL(crc32(0, mpt::byte_cast<const unsigned char*>(std::string("123456789").c_str()), 9), 0xCBF43926u);
  815. #endif
  816. #ifdef MPT_WITH_MINIZ
  817. VERIFY_EQUAL(mz_crc32(0, mpt::byte_cast<const unsigned char*>(std::string("123456789").c_str()), 9), 0xCBF43926u);
  818. #endif
  819. // Check floating-point accuracy in TransposeToFrequency
  820. static constexpr int32 transposeToFrequency[] =
  821. {
  822. 5, 5, 5, 5,
  823. 31, 32, 33, 34,
  824. 196, 202, 207, 214,
  825. 1243, 1280, 1317, 1356,
  826. 7894, 8125, 8363, 8608,
  827. 50121, 51590, 53102, 54658,
  828. 318251, 327576, 337175, 347055,
  829. 2020767, 2079980, 2140928, 2203663,
  830. };
  831. int freqIndex = 0;
  832. for(int32 transpose = -128; transpose < 128; transpose += 32)
  833. {
  834. for(int32 finetune = -128; finetune < 128; finetune += 64, freqIndex++)
  835. {
  836. const auto freq = ModSample::TransposeToFrequency(transpose, finetune);
  837. VERIFY_EQUAL_EPS(transposeToFrequency[freqIndex], static_cast<int32>(freq), 1);
  838. if(transpose >= -96)
  839. {
  840. // Verify transpose+finetune <-> frequency roundtrip
  841. // (not for transpose = -128 because it would require fractional precision that we don't have here)
  842. ModSample smp;
  843. smp.nC5Speed = freq;
  844. smp.FrequencyToTranspose();
  845. smp.TransposeToFrequency();
  846. VERIFY_EQUAL(freq, smp.nC5Speed);
  847. }
  848. }
  849. }
  850. {
  851. ModSample smp;
  852. smp.nC5Speed = 9999;
  853. smp.Transpose(1.5);
  854. VERIFY_EQUAL_EPS(28281, static_cast<int32>(smp.nC5Speed), 1);
  855. smp.Transpose(-1.3);
  856. VERIFY_EQUAL_EPS(11486, static_cast<int32>(smp.nC5Speed), 1);
  857. }
  858. // Check SamplePosition fixed-point type
  859. VERIFY_EQUAL(SamplePosition(1).GetRaw(), 1);
  860. VERIFY_EQUAL(SamplePosition(2).Set(1), SamplePosition(1, 0));
  861. VERIFY_EQUAL(SamplePosition(2).SetInt(1), SamplePosition(1, 2));
  862. VERIFY_EQUAL(SamplePosition(1).IsPositive(), true);
  863. VERIFY_EQUAL(SamplePosition(0).IsZero(), true);
  864. VERIFY_EQUAL(SamplePosition(1, 0).IsUnity(), true);
  865. VERIFY_EQUAL(SamplePosition(1, 1).IsUnity(), false);
  866. VERIFY_EQUAL(SamplePosition(-1).IsNegative(), true);
  867. VERIFY_EQUAL(SamplePosition(int64_max).GetRaw(), int64_max);
  868. VERIFY_EQUAL(SamplePosition(2, SamplePosition::fractMax).GetInt(), 2);
  869. VERIFY_EQUAL(SamplePosition(2, SamplePosition::fractMax).GetFract(), SamplePosition::fractMax);
  870. VERIFY_EQUAL(SamplePosition(1, SamplePosition::fractMax).GetInvertedFract(), SamplePosition(0, 1));
  871. VERIFY_EQUAL(SamplePosition(1, 0).GetInvertedFract(), SamplePosition(1, 0));
  872. VERIFY_EQUAL(SamplePosition(2, 0).Negate(), SamplePosition(-2, 0));
  873. VERIFY_EQUAL(SamplePosition::Ratio(10, 5), SamplePosition(2, 0));
  874. VERIFY_EQUAL(SamplePosition(1, 1) + SamplePosition(2, 2), SamplePosition(3, 3));
  875. VERIFY_EQUAL(SamplePosition(1, 0) * 3, SamplePosition(3, 0));
  876. VERIFY_EQUAL((SamplePosition(6, 0) / SamplePosition(2, 0)), 3);
  877. VERIFY_EQUAL(srlztn::ID::FromInt(static_cast<uint32>(0x87654321u)).AsString(), srlztn::ID("\x21\x43\x65\x87").AsString());
  878. #if defined(MODPLUG_TRACKER)
  879. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("1.1.44" )).AsString() , U_("1.1.44"));
  880. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("1.6.2" )).AsString() , U_("1.6.2" ));
  881. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("1.8" )).AsString() , U_("1.8.0" ));
  882. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("2.0-rc" )).AsString() , U_("2.0.0" ));
  883. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("2.0-rc4")).AsString() , U_("2.0.0" ));
  884. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("2.0" )).AsString() , U_("2.0.0" ));
  885. VERIFY_EQUAL(mpt::OS::Wine::Version(U_("2.4" )).AsString() , U_("2.4.0" ));
  886. #endif // MODPLUG_TRACKER
  887. // date
  888. VERIFY_EQUAL( 0, TestDate1( 0, 0, 0, 1, 1, 1970 ));
  889. VERIFY_EQUAL( 3600, TestDate1( 0, 0, 1, 1, 1, 1970 ));
  890. VERIFY_EQUAL( 86400, TestDate1( 0, 0, 0, 2, 1, 1970 ));
  891. VERIFY_EQUAL( 31536000, TestDate1( 0, 0, 0, 1, 1, 1971 ));
  892. VERIFY_EQUAL( 100000000, TestDate1( 40, 46, 9, 3, 3, 1973 ));
  893. VERIFY_EQUAL( 951782400, TestDate1( 0, 0, 0, 29, 2, 2000 ));
  894. VERIFY_EQUAL( 1000000000, TestDate1( 40, 46, 1, 9, 9, 2001 ));
  895. VERIFY_EQUAL( 1044057600, TestDate1( 0, 0, 0, 1, 2, 2003 ));
  896. VERIFY_EQUAL( 1044144000, TestDate1( 0, 0, 0, 2, 2, 2003 ));
  897. VERIFY_EQUAL( 1046476800, TestDate1( 0, 0, 0, 1, 3, 2003 ));
  898. VERIFY_EQUAL( 1064966400, TestDate1( 0, 0, 0, 1, 10, 2003 ));
  899. VERIFY_EQUAL( 1077926399, TestDate1( 59, 59, 23, 27, 2, 2004 ));
  900. VERIFY_EQUAL( 1077926400, TestDate1( 0, 0, 0, 28, 2, 2004 ));
  901. VERIFY_EQUAL( 1077926410, TestDate1( 10, 0, 0, 28, 2, 2004 ));
  902. VERIFY_EQUAL( 1078012799, TestDate1( 59, 59, 23, 28, 2, 2004 ));
  903. VERIFY_EQUAL( 1078012800, TestDate1( 0, 0, 0, 29, 2, 2004 ));
  904. VERIFY_EQUAL( 1078012820, TestDate1( 20, 0, 0, 29, 2, 2004 ));
  905. VERIFY_EQUAL( 1078099199, TestDate1( 59, 59, 23, 29, 2, 2004 ));
  906. VERIFY_EQUAL( 1078099200, TestDate1( 0, 0, 0, 1, 3, 2004 ));
  907. VERIFY_EQUAL( 1078099230, TestDate1( 30, 0, 0, 1, 3, 2004 ));
  908. VERIFY_EQUAL( 1078185599, TestDate1( 59, 59, 23, 1, 3, 2004 ));
  909. VERIFY_EQUAL( 1096588800, TestDate1( 0, 0, 0, 1, 10, 2004 ));
  910. VERIFY_EQUAL( 1413064016, TestDate1( 56, 46, 21, 11, 10, 2014 ));
  911. VERIFY_EQUAL( 1413064100, TestDate1( 20, 48, 21, 11, 10, 2014 ));
  912. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 0).AsUTC()), TestDate2( 0, 0, 0, 1, 1, 1970 ));
  913. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 3600).AsUTC()), TestDate2( 0, 0, 1, 1, 1, 1970 ));
  914. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 86400).AsUTC()), TestDate2( 0, 0, 0, 2, 1, 1970 ));
  915. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 31536000).AsUTC()), TestDate2( 0, 0, 0, 1, 1, 1971 ));
  916. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 100000000).AsUTC()), TestDate2( 40, 46, 9, 3, 3, 1973 ));
  917. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 951782400).AsUTC()), TestDate2( 0, 0, 0, 29, 2, 2000 ));
  918. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1000000000).AsUTC()), TestDate2( 40, 46, 1, 9, 9, 2001 ));
  919. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1044057600).AsUTC()), TestDate2( 0, 0, 0, 1, 2, 2003 ));
  920. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1044144000).AsUTC()), TestDate2( 0, 0, 0, 2, 2, 2003 ));
  921. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1046476800).AsUTC()), TestDate2( 0, 0, 0, 1, 3, 2003 ));
  922. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1064966400).AsUTC()), TestDate2( 0, 0, 0, 1, 10, 2003 ));
  923. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1077926399).AsUTC()), TestDate2( 59, 59, 23, 27, 2, 2004 ));
  924. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1077926400).AsUTC()), TestDate2( 0, 0, 0, 28, 2, 2004 ));
  925. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1077926410).AsUTC()), TestDate2( 10, 0, 0, 28, 2, 2004 ));
  926. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078012799).AsUTC()), TestDate2( 59, 59, 23, 28, 2, 2004 ));
  927. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078012800).AsUTC()), TestDate2( 0, 0, 0, 29, 2, 2004 ));
  928. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078012820).AsUTC()), TestDate2( 20, 0, 0, 29, 2, 2004 ));
  929. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078099199).AsUTC()), TestDate2( 59, 59, 23, 29, 2, 2004 ));
  930. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078099200).AsUTC()), TestDate2( 0, 0, 0, 1, 3, 2004 ));
  931. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078099230).AsUTC()), TestDate2( 30, 0, 0, 1, 3, 2004 ));
  932. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1078185599).AsUTC()), TestDate2( 59, 59, 23, 1, 3, 2004 ));
  933. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1096588800).AsUTC()), TestDate2( 0, 0, 0, 1, 10, 2004 ));
  934. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1413064016).AsUTC()), TestDate2( 56, 46, 21, 11, 10, 2014 ));
  935. VERIFY_EQUAL(Gregorian::FromTM(mpt::Date::Unix( 1413064100).AsUTC()), TestDate2( 20, 48, 21, 11, 10, 2014 ));
  936. #ifdef MODPLUG_TRACKER
  937. // URI & HTTP
  938. {
  939. URI uri = ParseURI(U_("scheme://username:password@host:port/path?query#fragment"));
  940. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  941. VERIFY_EQUAL(uri.username, U_("username"));
  942. VERIFY_EQUAL(uri.password, U_("password"));
  943. VERIFY_EQUAL(uri.host, U_("host"));
  944. VERIFY_EQUAL(uri.port, U_("port"));
  945. VERIFY_EQUAL(uri.path, U_("/path"));
  946. VERIFY_EQUAL(uri.query, U_("query"));
  947. VERIFY_EQUAL(uri.fragment, U_("fragment"));
  948. }
  949. {
  950. URI uri = ParseURI(U_("scheme://host/path"));
  951. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  952. VERIFY_EQUAL(uri.username, U_(""));
  953. VERIFY_EQUAL(uri.password, U_(""));
  954. VERIFY_EQUAL(uri.host, U_("host"));
  955. VERIFY_EQUAL(uri.port, U_(""));
  956. VERIFY_EQUAL(uri.path, U_("/path"));
  957. VERIFY_EQUAL(uri.query, U_(""));
  958. VERIFY_EQUAL(uri.fragment, U_(""));
  959. }
  960. {
  961. URI uri = ParseURI(U_("scheme://host"));
  962. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  963. VERIFY_EQUAL(uri.username, U_(""));
  964. VERIFY_EQUAL(uri.password, U_(""));
  965. VERIFY_EQUAL(uri.host, U_("host"));
  966. VERIFY_EQUAL(uri.port, U_(""));
  967. VERIFY_EQUAL(uri.path, U_(""));
  968. VERIFY_EQUAL(uri.query, U_(""));
  969. VERIFY_EQUAL(uri.fragment, U_(""));
  970. }
  971. {
  972. URI uri = ParseURI(U_("scheme://host?query"));
  973. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  974. VERIFY_EQUAL(uri.username, U_(""));
  975. VERIFY_EQUAL(uri.password, U_(""));
  976. VERIFY_EQUAL(uri.host, U_("host"));
  977. VERIFY_EQUAL(uri.port, U_(""));
  978. VERIFY_EQUAL(uri.path, U_(""));
  979. VERIFY_EQUAL(uri.query, U_("query"));
  980. VERIFY_EQUAL(uri.fragment, U_(""));
  981. }
  982. {
  983. URI uri = ParseURI(U_("scheme://host#fragment"));
  984. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  985. VERIFY_EQUAL(uri.username, U_(""));
  986. VERIFY_EQUAL(uri.password, U_(""));
  987. VERIFY_EQUAL(uri.host, U_("host"));
  988. VERIFY_EQUAL(uri.port, U_(""));
  989. VERIFY_EQUAL(uri.path, U_(""));
  990. VERIFY_EQUAL(uri.query, U_(""));
  991. VERIFY_EQUAL(uri.fragment, U_("fragment"));
  992. }
  993. {
  994. URI uri = ParseURI(U_("scheme://host?#"));
  995. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  996. VERIFY_EQUAL(uri.username, U_(""));
  997. VERIFY_EQUAL(uri.password, U_(""));
  998. VERIFY_EQUAL(uri.host, U_("host"));
  999. VERIFY_EQUAL(uri.port, U_(""));
  1000. VERIFY_EQUAL(uri.path, U_(""));
  1001. VERIFY_EQUAL(uri.query, U_(""));
  1002. VERIFY_EQUAL(uri.fragment, U_(""));
  1003. }
  1004. {
  1005. URI uri = ParseURI(U_("scheme://host#?"));
  1006. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  1007. VERIFY_EQUAL(uri.username, U_(""));
  1008. VERIFY_EQUAL(uri.password, U_(""));
  1009. VERIFY_EQUAL(uri.host, U_("host"));
  1010. VERIFY_EQUAL(uri.port, U_(""));
  1011. VERIFY_EQUAL(uri.path, U_(""));
  1012. VERIFY_EQUAL(uri.query, U_(""));
  1013. VERIFY_EQUAL(uri.fragment, U_("?"));
  1014. }
  1015. {
  1016. URI uri = ParseURI(U_("scheme://username:password@[2001:db8::1]:port/path?query#fragment"));
  1017. VERIFY_EQUAL(uri.scheme, U_("scheme"));
  1018. VERIFY_EQUAL(uri.username, U_("username"));
  1019. VERIFY_EQUAL(uri.password, U_("password"));
  1020. VERIFY_EQUAL(uri.host, U_("[2001:db8::1]"));
  1021. VERIFY_EQUAL(uri.port, U_("port"));
  1022. VERIFY_EQUAL(uri.path, U_("/path"));
  1023. VERIFY_EQUAL(uri.query, U_("query"));
  1024. VERIFY_EQUAL(uri.fragment, U_("fragment"));
  1025. }
  1026. {
  1027. URI uri = ParseURI(U_("https://[email protected]:123/forum/questions/?tag=networking&order=newest#top"));
  1028. VERIFY_EQUAL(uri.scheme, U_("https"));
  1029. VERIFY_EQUAL(uri.username, U_("john.doe"));
  1030. VERIFY_EQUAL(uri.password, U_(""));
  1031. VERIFY_EQUAL(uri.host, U_("www.example.com"));
  1032. VERIFY_EQUAL(uri.port, U_("123"));
  1033. VERIFY_EQUAL(uri.path, U_("/forum/questions/"));
  1034. VERIFY_EQUAL(uri.query, U_("tag=networking&order=newest"));
  1035. VERIFY_EQUAL(uri.fragment, U_("top"));
  1036. }
  1037. {
  1038. URI uri = ParseURI(U_("ldap://[2001:db8::7]/c=GB?objectClass?one"));
  1039. VERIFY_EQUAL(uri.scheme, U_("ldap"));
  1040. VERIFY_EQUAL(uri.username, U_(""));
  1041. VERIFY_EQUAL(uri.password, U_(""));
  1042. VERIFY_EQUAL(uri.host, U_("[2001:db8::7]"));
  1043. VERIFY_EQUAL(uri.port, U_(""));
  1044. VERIFY_EQUAL(uri.path, U_("/c=GB"));
  1045. VERIFY_EQUAL(uri.query, U_("objectClass?one"));
  1046. VERIFY_EQUAL(uri.fragment, U_(""));
  1047. }
  1048. {
  1049. URI uri = ParseURI(U_("mailto:[email protected]"));
  1050. VERIFY_EQUAL(uri.scheme, U_("mailto"));
  1051. VERIFY_EQUAL(uri.username, U_(""));
  1052. VERIFY_EQUAL(uri.password, U_(""));
  1053. VERIFY_EQUAL(uri.host, U_(""));
  1054. VERIFY_EQUAL(uri.port, U_(""));
  1055. VERIFY_EQUAL(uri.path, U_("[email protected]"));
  1056. VERIFY_EQUAL(uri.query, U_(""));
  1057. VERIFY_EQUAL(uri.fragment, U_(""));
  1058. }
  1059. {
  1060. URI uri = ParseURI(U_("news:comp.infosystems.www.servers.unix"));
  1061. VERIFY_EQUAL(uri.scheme, U_("news"));
  1062. VERIFY_EQUAL(uri.username, U_(""));
  1063. VERIFY_EQUAL(uri.password, U_(""));
  1064. VERIFY_EQUAL(uri.host, U_(""));
  1065. VERIFY_EQUAL(uri.port, U_(""));
  1066. VERIFY_EQUAL(uri.path, U_("comp.infosystems.www.servers.unix"));
  1067. VERIFY_EQUAL(uri.query, U_(""));
  1068. VERIFY_EQUAL(uri.fragment, U_(""));
  1069. }
  1070. {
  1071. URI uri = ParseURI(U_("tel:+1-816-555-1212"));
  1072. VERIFY_EQUAL(uri.scheme, U_("tel"));
  1073. VERIFY_EQUAL(uri.username, U_(""));
  1074. VERIFY_EQUAL(uri.password, U_(""));
  1075. VERIFY_EQUAL(uri.host, U_(""));
  1076. VERIFY_EQUAL(uri.port, U_(""));
  1077. VERIFY_EQUAL(uri.path, U_("+1-816-555-1212"));
  1078. VERIFY_EQUAL(uri.query, U_(""));
  1079. VERIFY_EQUAL(uri.fragment, U_(""));
  1080. }
  1081. {
  1082. URI uri = ParseURI(U_("telnet://192.0.2.16:80/"));
  1083. VERIFY_EQUAL(uri.scheme, U_("telnet"));
  1084. VERIFY_EQUAL(uri.username, U_(""));
  1085. VERIFY_EQUAL(uri.password, U_(""));
  1086. VERIFY_EQUAL(uri.host, U_("192.0.2.16"));
  1087. VERIFY_EQUAL(uri.port, U_("80"));
  1088. VERIFY_EQUAL(uri.path, U_("/"));
  1089. VERIFY_EQUAL(uri.query, U_(""));
  1090. VERIFY_EQUAL(uri.fragment, U_(""));
  1091. }
  1092. {
  1093. URI uri = ParseURI(U_("urn:oasis:names:specification:docbook:dtd:xml:4.1.2"));
  1094. VERIFY_EQUAL(uri.scheme, U_("urn"));
  1095. VERIFY_EQUAL(uri.username, U_(""));
  1096. VERIFY_EQUAL(uri.password, U_(""));
  1097. VERIFY_EQUAL(uri.host, U_(""));
  1098. VERIFY_EQUAL(uri.port, U_(""));
  1099. VERIFY_EQUAL(uri.path, U_("oasis:names:specification:docbook:dtd:xml:4.1.2"));
  1100. VERIFY_EQUAL(uri.query, U_(""));
  1101. VERIFY_EQUAL(uri.fragment, U_(""));
  1102. }
  1103. {
  1104. HTTP::Request req;
  1105. req.SetURI(ParseURI(U_("https://host/path?a1=a&a2=b")));
  1106. VERIFY_EQUAL(req.protocol, HTTP::Protocol::HTTPS);
  1107. VERIFY_EQUAL(req.host, U_("host"));
  1108. VERIFY_EQUAL(req.path, U_("/path"));
  1109. VERIFY_EQUAL(req.query.size(), 2u);
  1110. if(req.query.size() == 2)
  1111. {
  1112. VERIFY_EQUAL(req.query[0], std::make_pair(U_("a1"), U_("a")));
  1113. VERIFY_EQUAL(req.query[1], std::make_pair(U_("a2"), U_("b")));
  1114. }
  1115. }
  1116. {
  1117. HTTP::Request req;
  1118. req.SetURI(ParseURI(U_("https://host/")));
  1119. VERIFY_EQUAL(req.protocol, HTTP::Protocol::HTTPS);
  1120. VERIFY_EQUAL(req.host, U_("host"));
  1121. VERIFY_EQUAL(req.path, U_("/"));
  1122. }
  1123. #endif // MODPLUG_TRACKER
  1124. // https://github.com/kripken/emscripten/issues/4251
  1125. #if MPT_OS_EMSCRIPTEN
  1126. volatile int transpose = 32;
  1127. volatile int finetune = 0;
  1128. float exp = (transpose * 128.0f + finetune) * (1.0f / (12.0f * 128.0f));
  1129. float f = ::powf(2.0f, exp);
  1130. double d = ::pow (2.0 , (double)exp);
  1131. VERIFY_EQUAL_EPS(d, 6.349605, 0.00001);
  1132. VERIFY_EQUAL_EPS(f, 6.349605, 0.00001);
  1133. #endif
  1134. }
  1135. static MPT_NOINLINE void TestRandom()
  1136. {
  1137. #ifdef FLAKY_TESTS
  1138. mpt::default_prng& prng = *s_PRNG;
  1139. {
  1140. std::vector<std::size_t> hist(256);
  1141. for(std::size_t i = 0; i < 256*256; ++i)
  1142. {
  1143. uint8 value = mpt::random<uint8>(prng);
  1144. hist[value] += 1;
  1145. }
  1146. for(std::size_t i = 0; i < 256; ++i)
  1147. {
  1148. VERIFY_EQUAL_QUIET_NONCONT(mpt::is_in_range(hist[i], 16u, 65520u), true);
  1149. }
  1150. }
  1151. {
  1152. std::vector<std::size_t> hist(256);
  1153. for(std::size_t i = 0; i < 256*256; ++i)
  1154. {
  1155. int8 value = mpt::random<int8>(prng);
  1156. hist[static_cast<int>(value) + 0x80] += 1;
  1157. }
  1158. for(std::size_t i = 0; i < 256; ++i)
  1159. {
  1160. VERIFY_EQUAL_QUIET_NONCONT(mpt::is_in_range(hist[i], 16u, 65520u), true);
  1161. }
  1162. }
  1163. {
  1164. std::vector<std::size_t> hist(256);
  1165. for(std::size_t i = 0; i < 256*256; ++i)
  1166. {
  1167. uint8 value = mpt::random<uint8>(prng, 1);
  1168. hist[value] += 1;
  1169. }
  1170. for(std::size_t i = 0; i < 256; ++i)
  1171. {
  1172. if(i < 2)
  1173. {
  1174. VERIFY_EQUAL_QUIET_NONCONT(mpt::is_in_range(hist[i], 16u, 65520u), true);
  1175. } else
  1176. {
  1177. VERIFY_EQUAL_QUIET_NONCONT(hist[i], 0u);
  1178. }
  1179. }
  1180. }
  1181. #endif
  1182. }
  1183. static MPT_NOINLINE void TestCharsets()
  1184. {
  1185. // Path splitting
  1186. #if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
  1187. VERIFY_EQUAL(P_("").GetDrive(), P_(""));
  1188. VERIFY_EQUAL(P_("").GetDir(), P_(""));
  1189. VERIFY_EQUAL(P_("").GetPath(), P_(""));
  1190. VERIFY_EQUAL(P_("").GetFileName(), P_(""));
  1191. VERIFY_EQUAL(P_("").GetFileExt(), P_(""));
  1192. VERIFY_EQUAL(P_("").GetFullFileName(), P_(""));
  1193. VERIFY_EQUAL(P_("C:\\").GetDrive(), P_("C:"));
  1194. VERIFY_EQUAL(P_("C:\\").GetDir(), P_("\\"));
  1195. VERIFY_EQUAL(P_("C:\\").GetPath(), P_("C:\\"));
  1196. VERIFY_EQUAL(P_("C:\\").GetFileName(), P_(""));
  1197. VERIFY_EQUAL(P_("C:\\").GetFileExt(), P_(""));
  1198. VERIFY_EQUAL(P_("C:\\").GetFullFileName(), P_(""));
  1199. VERIFY_EQUAL(P_("\\directory\\").GetDrive(), P_(""));
  1200. VERIFY_EQUAL(P_("\\directory\\").GetDir(), P_("\\directory\\"));
  1201. VERIFY_EQUAL(P_("\\directory\\").GetPath(), P_("\\directory\\"));
  1202. VERIFY_EQUAL(P_("\\directory\\").GetFileName(), P_(""));
  1203. VERIFY_EQUAL(P_("\\directory\\").GetFileExt(), P_(""));
  1204. VERIFY_EQUAL(P_("\\directory\\").GetFullFileName(), P_(""));
  1205. VERIFY_EQUAL(P_("\\directory\\file.txt").GetDrive(), P_(""));
  1206. VERIFY_EQUAL(P_("\\directory\\file.txt").GetDir(), P_("\\directory\\"));
  1207. VERIFY_EQUAL(P_("\\directory\\file.txt").GetPath(), P_("\\directory\\"));
  1208. VERIFY_EQUAL(P_("\\directory\\file.txt").GetFileName(), P_("file"));
  1209. VERIFY_EQUAL(P_("\\directory\\file.txt").GetFileExt(), P_(".txt"));
  1210. VERIFY_EQUAL(P_("\\directory\\file.txt").GetFullFileName(), P_("file.txt"));
  1211. VERIFY_EQUAL(P_(".").GetDrive(), P_(""));
  1212. VERIFY_EQUAL(P_(".").GetDir(), P_(""));
  1213. VERIFY_EQUAL(P_(".").GetPath(), P_(""));
  1214. VERIFY_EQUAL(P_(".").GetFileName(), P_("."));
  1215. VERIFY_EQUAL(P_(".").GetFileExt(), P_(""));
  1216. VERIFY_EQUAL(P_(".").GetFullFileName(), P_("."));
  1217. VERIFY_EQUAL(P_("..").GetDrive(), P_(""));
  1218. VERIFY_EQUAL(P_("..").GetDir(), P_(""));
  1219. VERIFY_EQUAL(P_("..").GetPath(), P_(""));
  1220. VERIFY_EQUAL(P_("..").GetFileName(), P_(".."));
  1221. VERIFY_EQUAL(P_("..").GetFileExt(), P_(""));
  1222. VERIFY_EQUAL(P_("..").GetFullFileName(), P_(".."));
  1223. VERIFY_EQUAL(P_("dir\\.").GetDrive(), P_(""));
  1224. VERIFY_EQUAL(P_("dir\\.").GetDir(), P_("dir\\"));
  1225. VERIFY_EQUAL(P_("dir\\.").GetPath(), P_("dir\\"));
  1226. VERIFY_EQUAL(P_("dir\\.").GetFileName(), P_("."));
  1227. VERIFY_EQUAL(P_("dir\\.").GetFileExt(), P_(""));
  1228. VERIFY_EQUAL(P_("dir\\.").GetFullFileName(), P_("."));
  1229. VERIFY_EQUAL(P_("dir\\..").GetDrive(), P_(""));
  1230. VERIFY_EQUAL(P_("dir\\..").GetDir(), P_("dir\\"));
  1231. VERIFY_EQUAL(P_("dir\\..").GetPath(), P_("dir\\"));
  1232. VERIFY_EQUAL(P_("dir\\..").GetFileName(), P_(".."));
  1233. VERIFY_EQUAL(P_("dir\\..").GetFileExt(), P_(""));
  1234. VERIFY_EQUAL(P_("dir\\..").GetFullFileName(), P_(".."));
  1235. VERIFY_EQUAL(P_(".txt").GetDrive(), P_(""));
  1236. VERIFY_EQUAL(P_(".txt").GetDir(), P_(""));
  1237. VERIFY_EQUAL(P_(".txt").GetPath(), P_(""));
  1238. VERIFY_EQUAL(P_(".txt").GetFileName(), P_(".txt"));
  1239. VERIFY_EQUAL(P_(".txt").GetFileExt(), P_(""));
  1240. VERIFY_EQUAL(P_(".txt").GetFullFileName(), P_(".txt"));
  1241. VERIFY_EQUAL(P_("C:tmp.txt").GetDrive(), P_("C:"));
  1242. VERIFY_EQUAL(P_("C:tmp.txt").GetDir(), P_(""));
  1243. VERIFY_EQUAL(P_("C:tmp.txt").GetPath(), P_("C:"));
  1244. VERIFY_EQUAL(P_("C:tmp.txt").GetFileName(), P_("tmp"));
  1245. VERIFY_EQUAL(P_("C:tmp.txt").GetFileExt(), P_(".txt"));
  1246. VERIFY_EQUAL(P_("C:tmp.txt").GetFullFileName(), P_("tmp.txt"));
  1247. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetDrive(), P_("C:"));
  1248. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetDir(), P_("tempdir\\"));
  1249. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetPath(), P_("C:tempdir\\"));
  1250. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetFileName(), P_("tmp"));
  1251. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetFileExt(), P_(".txt"));
  1252. VERIFY_EQUAL(P_("C:tempdir\\tmp.txt").GetFullFileName(), P_("tmp.txt"));
  1253. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetDrive(), P_("C:"));
  1254. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetDir(), P_("\\tempdir\\"));
  1255. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetPath(), P_("C:\\tempdir\\"));
  1256. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetFileName(), P_("tmp"));
  1257. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetFileExt(), P_(".txt"));
  1258. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.txt").GetFullFileName(), P_("tmp.txt"));
  1259. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.foo.txt").GetFileName(), P_("tmp.foo"));
  1260. VERIFY_EQUAL(P_("C:\\tempdir\\tmp.foo.txt").GetFileExt(), P_(".txt"));
  1261. VERIFY_EQUAL(P_("\\\\server").GetDrive(), P_("\\\\server"));
  1262. VERIFY_EQUAL(P_("\\\\server").GetDir(), P_(""));
  1263. VERIFY_EQUAL(P_("\\\\server").GetPath(), P_("\\\\server"));
  1264. VERIFY_EQUAL(P_("\\\\server").GetFileName(), P_(""));
  1265. VERIFY_EQUAL(P_("\\\\server").GetFileExt(), P_(""));
  1266. VERIFY_EQUAL(P_("\\\\server").GetFullFileName(), P_(""));
  1267. VERIFY_EQUAL(P_("\\\\server\\").GetDrive(), P_("\\\\server\\"));
  1268. VERIFY_EQUAL(P_("\\\\server\\").GetDir(), P_(""));
  1269. VERIFY_EQUAL(P_("\\\\server\\").GetPath(), P_("\\\\server\\"));
  1270. VERIFY_EQUAL(P_("\\\\server\\").GetFileName(), P_(""));
  1271. VERIFY_EQUAL(P_("\\\\server\\").GetFileExt(), P_(""));
  1272. VERIFY_EQUAL(P_("\\\\server\\").GetFullFileName(), P_(""));
  1273. VERIFY_EQUAL(P_("\\\\server\\share").GetDrive(), P_("\\\\server\\share"));
  1274. VERIFY_EQUAL(P_("\\\\server\\share").GetDir(), P_(""));
  1275. VERIFY_EQUAL(P_("\\\\server\\share").GetPath(), P_("\\\\server\\share"));
  1276. VERIFY_EQUAL(P_("\\\\server\\share").GetFileName(), P_(""));
  1277. VERIFY_EQUAL(P_("\\\\server\\share").GetFileExt(), P_(""));
  1278. VERIFY_EQUAL(P_("\\\\server\\share").GetFullFileName(), P_(""));
  1279. VERIFY_EQUAL(P_("\\\\server\\share\\").GetDrive(), P_("\\\\server\\share"));
  1280. VERIFY_EQUAL(P_("\\\\server\\share\\").GetDir(), P_("\\"));
  1281. VERIFY_EQUAL(P_("\\\\server\\share\\").GetPath(), P_("\\\\server\\share\\"));
  1282. VERIFY_EQUAL(P_("\\\\server\\share\\").GetFileName(), P_(""));
  1283. VERIFY_EQUAL(P_("\\\\server\\share\\").GetFileExt(), P_(""));
  1284. VERIFY_EQUAL(P_("\\\\server\\share\\").GetFullFileName(), P_(""));
  1285. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetDrive(), P_("\\\\server\\share"));
  1286. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetDir(), P_("\\dir1\\dir2\\"));
  1287. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetPath(), P_("\\\\server\\share\\dir1\\dir2\\"));
  1288. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetFileName(), P_("name.foo"));
  1289. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetFileExt(), P_(".ext"));
  1290. VERIFY_EQUAL(P_("\\\\server\\share\\dir1\\dir2\\name.foo.ext").GetFullFileName(), P_("name.foo.ext"));
  1291. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetDrive(), P_("C:"));
  1292. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetDir(), P_("\\tempdir\\dir.2\\"));
  1293. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetPath(), P_("C:\\tempdir\\dir.2\\"));
  1294. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetFileName(), P_("tmp.foo"));
  1295. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetFileExt(), P_(".txt"));
  1296. VERIFY_EQUAL(P_("\\\\?\\C:\\tempdir\\dir.2\\tmp.foo.txt").GetFullFileName(), P_("tmp.foo.txt"));
  1297. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetDrive(), P_("\\\\server\\share"));
  1298. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetDir(), P_("\\dir1\\dir2\\"));
  1299. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetPath(), P_("\\\\server\\share\\dir1\\dir2\\"));
  1300. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetFileName(), P_("name.foo"));
  1301. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetFileExt(), P_(".ext"));
  1302. VERIFY_EQUAL(P_("\\\\?\\UNC\\server\\share\\dir1\\dir2\\name.foo.ext").GetFullFileName(), P_("name.foo.ext"));
  1303. #endif
  1304. // Path conversions
  1305. #ifdef MODPLUG_TRACKER
  1306. const mpt::PathString exePath = P_("C:\\OpenMPT\\");
  1307. VERIFY_EQUAL(P_("C:\\OpenMPT\\").AbsolutePathToRelative(exePath), P_(".\\"));
  1308. VERIFY_EQUAL(P_("c:\\OpenMPT\\foo").AbsolutePathToRelative(exePath), P_(".\\foo"));
  1309. VERIFY_EQUAL(P_("C:\\foo").AbsolutePathToRelative(exePath), P_("\\foo"));
  1310. VERIFY_EQUAL(P_(".\\").RelativePathToAbsolute(exePath), P_("C:\\OpenMPT\\"));
  1311. VERIFY_EQUAL(P_(".\\foo").RelativePathToAbsolute(exePath), P_("C:\\OpenMPT\\foo"));
  1312. VERIFY_EQUAL(P_("\\foo").RelativePathToAbsolute(exePath), P_("C:\\foo"));
  1313. VERIFY_EQUAL(P_("\\\\server\\path\\file").AbsolutePathToRelative(exePath), P_("\\\\server\\path\\file"));
  1314. VERIFY_EQUAL(P_("\\\\server\\path\\file").RelativePathToAbsolute(exePath), P_("\\\\server\\path\\file"));
  1315. VERIFY_EQUAL(P_("").Simplify(), P_(""));
  1316. VERIFY_EQUAL(P_(" ").Simplify(), P_(" "));
  1317. VERIFY_EQUAL(P_("foo\\bar").Simplify(), P_("foo\\bar"));
  1318. VERIFY_EQUAL(P_(".\\foo\\bar").Simplify(), P_(".\\foo\\bar"));
  1319. VERIFY_EQUAL(P_(".\\\\foo\\bar").Simplify(), P_(".\\foo\\bar"));
  1320. VERIFY_EQUAL(P_("./\\foo\\bar").Simplify(), P_(".\\foo\\bar"));
  1321. VERIFY_EQUAL(P_("\\foo\\bar").Simplify(), P_("\\foo\\bar"));
  1322. VERIFY_EQUAL(P_("A:\\name_1\\.\\name_2\\..\\name_3\\").Simplify(), P_("A:\\name_1\\name_3"));
  1323. VERIFY_EQUAL(P_("A:\\name_1\\..\\name_2\\./name_3").Simplify(), P_("A:\\name_2\\name_3"));
  1324. VERIFY_EQUAL(P_("A:\\name_1\\.\\name_2\\.\\name_3\\..\\name_4\\..").Simplify(), P_("A:\\name_1\\name_2"));
  1325. VERIFY_EQUAL(P_("A:foo\\\\bar").Simplify(), P_("A:\\foo\\bar"));
  1326. VERIFY_EQUAL(P_("C:\\..").Simplify(), P_("C:\\"));
  1327. VERIFY_EQUAL(P_("C:\\.").Simplify(), P_("C:\\"));
  1328. VERIFY_EQUAL(P_("\\\\foo\\..\\.bar").Simplify(), P_("\\\\.bar"));
  1329. VERIFY_EQUAL(P_("\\\\foo\\..\\..\\bar").Simplify(), P_("\\\\bar"));
  1330. #endif
  1331. #ifdef MODPLUG_TRACKER
  1332. #if MPT_COMPILER_MSVC
  1333. // Tracker code currently relies on BROKEN MSVC behaviour with respect to %s
  1334. // handling in printf functions when the target is a wide string.
  1335. // Ensure that Microsoft KEEPS this BROKEN behavour (it conflicts with C99 and
  1336. // C++11 demanded semantics.
  1337. // Additionally, the tracker code makes use of %hs and %ls which are the only
  1338. // way to unconditionally expect narrow or wide string parameters
  1339. // respectively; %ls is standard conforming, %hs is a Microsoft extension.
  1340. // We check all possible combinations and expect Microsoft-style behaviour
  1341. // here.
  1342. char src_char[256];
  1343. wchar_t src_wchar_t[256];
  1344. TCHAR src_TCHAR[256];
  1345. char dst_char[256];
  1346. wchar_t dst_wchar_t[256];
  1347. TCHAR dst_TCHAR[256];
  1348. MemsetZero(src_char);
  1349. MemsetZero(src_wchar_t);
  1350. MemsetZero(src_TCHAR);
  1351. strcpy(src_char, "ab");
  1352. wcscpy(src_wchar_t, L"ab");
  1353. _tcscpy(src_TCHAR, _T("ab"));
  1354. #define MPT_TEST_PRINTF(dst_type, function, format, src_type) \
  1355. do { \
  1356. MemsetZero(dst_ ## dst_type); \
  1357. function(dst_ ## dst_type, format, src_ ## src_type); \
  1358. VERIFY_EQUAL(std::char_traits< dst_type >::compare(dst_ ## dst_type, src_ ## dst_type, std::char_traits< dst_type >::length( src_ ## dst_type ) + 1), 0); \
  1359. } while(0) \
  1360. /**/
  1361. #define MPT_TEST_PRINTF_N(dst_type, function, format, src_type) \
  1362. do { \
  1363. MemsetZero(dst_ ## dst_type); \
  1364. function(dst_ ## dst_type, 255, format, src_ ## src_type); \
  1365. VERIFY_EQUAL(std::char_traits< dst_type >::compare(dst_ ## dst_type, src_ ## dst_type, std::char_traits< dst_type >::length( src_ ## dst_type ) + 1), 0); \
  1366. } while(0) \
  1367. /**/
  1368. // CRT narrow
  1369. MPT_TEST_PRINTF(char, sprintf, "%s", char);
  1370. MPT_TEST_PRINTF(char, sprintf, "%S", wchar_t);
  1371. MPT_TEST_PRINTF(char, sprintf, "%hs", char);
  1372. MPT_TEST_PRINTF(char, sprintf, "%hS", char);
  1373. MPT_TEST_PRINTF(char, sprintf, "%ls", wchar_t);
  1374. MPT_TEST_PRINTF(char, sprintf, "%lS", wchar_t);
  1375. MPT_TEST_PRINTF(char, sprintf, "%ws", wchar_t);
  1376. MPT_TEST_PRINTF(char, sprintf, "%wS", wchar_t);
  1377. // CRT wide
  1378. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%s", wchar_t);
  1379. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%S", char);
  1380. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%hs", char);
  1381. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%hS", char);
  1382. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%ls", wchar_t);
  1383. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%lS", wchar_t);
  1384. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%ws", wchar_t);
  1385. MPT_TEST_PRINTF_N(wchar_t, swprintf, L"%wS", wchar_t);
  1386. // WinAPI TCHAR
  1387. MPT_TEST_PRINTF(TCHAR, wsprintf, _T("%s"), TCHAR);
  1388. MPT_TEST_PRINTF(TCHAR, wsprintf, _T("%hs"), char);
  1389. MPT_TEST_PRINTF(TCHAR, wsprintf, _T("%hS"), char);
  1390. MPT_TEST_PRINTF(TCHAR, wsprintf, _T("%ls"), wchar_t);
  1391. MPT_TEST_PRINTF(TCHAR, wsprintf, _T("%lS"), wchar_t);
  1392. // WinAPI CHAR
  1393. MPT_TEST_PRINTF(char, wsprintfA, "%s", char);
  1394. MPT_TEST_PRINTF(char, wsprintfA, "%S", wchar_t);
  1395. MPT_TEST_PRINTF(char, wsprintfA, "%hs", char);
  1396. MPT_TEST_PRINTF(char, wsprintfA, "%hS", char);
  1397. MPT_TEST_PRINTF(char, wsprintfA, "%ls", wchar_t);
  1398. MPT_TEST_PRINTF(char, wsprintfA, "%lS", wchar_t);
  1399. // WinAPI WCHAR
  1400. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%s", wchar_t);
  1401. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%S", char);
  1402. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%hs", char);
  1403. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%hS", char);
  1404. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%ls", wchar_t);
  1405. MPT_TEST_PRINTF(wchar_t, wsprintfW, L"%lS", wchar_t);
  1406. #undef MPT_TEST_PRINTF
  1407. #undef MPT_TEST_PRINTF_n
  1408. #endif
  1409. #endif
  1410. VERIFY_EQUAL(mpt::CompareNoCaseAscii("f", "f", 6) == 0, true);
  1411. VERIFY_EQUAL(mpt::CompareNoCaseAscii("f", "F", 6) == 0, true);
  1412. VERIFY_EQUAL(mpt::CompareNoCaseAscii("F", "f", 6) == 0, true);
  1413. VERIFY_EQUAL(mpt::CompareNoCaseAscii("f", "g", 6) < 0, true);
  1414. VERIFY_EQUAL(mpt::CompareNoCaseAscii("h", "g", 6) > 0, true);
  1415. VERIFY_EQUAL(mpt::CompareNoCaseAscii("fgh", "FgH", 6) == 0, true);
  1416. VERIFY_EQUAL(mpt::CompareNoCaseAscii("012345678", "012345678", 9) == 0, true);
  1417. VERIFY_EQUAL(mpt::CompareNoCaseAscii("", "012345678", 9) < 0, true);
  1418. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "", 6) > 0, true);
  1419. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "F", 6) > 0, true);
  1420. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "Fg", 6) > 0, true);
  1421. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fg", 6) > 0, true);
  1422. VERIFY_EQUAL(mpt::CompareNoCaseAscii("0123456789", "FgH", 0) == 0, true);
  1423. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fgh", 1) == 0, true);
  1424. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fgh", 2) == 0, true);
  1425. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fgh", 3) == 0, true);
  1426. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fghi", 3) == 0, true);
  1427. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FgH", "fghi", 4) < 0, true);
  1428. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FIH", "fghi", 1) == 0, true);
  1429. VERIFY_EQUAL(mpt::CompareNoCaseAscii("FIH", "fghi", 2) > 0, true);
  1430. }
  1431. #ifdef MODPLUG_TRACKER
  1432. struct CustomSettingsTestType
  1433. {
  1434. float x;
  1435. float y;
  1436. CustomSettingsTestType(float x_ = 0.0f, float y_ = 0.0f) : x(x_), y(y_) { }
  1437. };
  1438. } // namespace Test
  1439. template <>
  1440. inline Test::CustomSettingsTestType FromSettingValue(const SettingValue &val)
  1441. {
  1442. MPT_ASSERT(val.GetTypeTag() == "myType");
  1443. mpt::ustring xy = val.as<mpt::ustring>();
  1444. if(xy.empty())
  1445. {
  1446. return Test::CustomSettingsTestType(0.0f, 0.0f);
  1447. }
  1448. std::size_t pos = xy.find(U_("|"));
  1449. mpt::ustring x = xy.substr(0, pos);
  1450. mpt::ustring y = xy.substr(pos + 1);
  1451. return Test::CustomSettingsTestType(ConvertStrTo<float>(x), ConvertStrTo<float>(y));
  1452. }
  1453. template <>
  1454. inline SettingValue ToSettingValue(const Test::CustomSettingsTestType &val)
  1455. {
  1456. return SettingValue(mpt::ufmt::val(val.x) + U_("|") + mpt::ufmt::val(val.y), "myType");
  1457. }
  1458. namespace Test {
  1459. #endif // MODPLUG_TRACKER
  1460. static MPT_NOINLINE void TestSettings()
  1461. {
  1462. #ifdef MODPLUG_TRACKER
  1463. VERIFY_EQUAL(SettingPath(U_("a"),U_("b")) < SettingPath(U_("a"),U_("c")), true);
  1464. VERIFY_EQUAL(!(SettingPath(U_("c"),U_("b")) < SettingPath(U_("a"),U_("c"))), true);
  1465. {
  1466. DefaultSettingsContainer conf;
  1467. int32 foobar = conf.Read(U_("Test"), U_("bar"), 23);
  1468. conf.Write(U_("Test"), U_("bar"), 64);
  1469. conf.Write(U_("Test"), U_("bar"), 42);
  1470. conf.Read(U_("Test"), U_("baz"), 4711);
  1471. foobar = conf.Read(U_("Test"), U_("bar"), 28);
  1472. }
  1473. {
  1474. DefaultSettingsContainer conf;
  1475. int32 foobar = conf.Read(U_("Test"), U_("bar"), 28);
  1476. VERIFY_EQUAL(foobar, 42);
  1477. conf.Write(U_("Test"), U_("bar"), 43);
  1478. }
  1479. {
  1480. DefaultSettingsContainer conf;
  1481. int32 foobar = conf.Read(U_("Test"), U_("bar"), 123);
  1482. VERIFY_EQUAL(foobar, 43);
  1483. conf.Write(U_("Test"), U_("bar"), 88);
  1484. }
  1485. {
  1486. DefaultSettingsContainer conf;
  1487. Setting<int> foo(conf, U_("Test"), U_("bar"), 99);
  1488. VERIFY_EQUAL(foo, 88);
  1489. foo = 7;
  1490. }
  1491. {
  1492. DefaultSettingsContainer conf;
  1493. Setting<int> foo(conf, U_("Test"), U_("bar"), 99);
  1494. VERIFY_EQUAL(foo, 7);
  1495. }
  1496. {
  1497. DefaultSettingsContainer conf;
  1498. conf.Read(U_("Test"), U_("struct"), std::string(""));
  1499. conf.Write(U_("Test"), U_("struct"), std::string(""));
  1500. }
  1501. {
  1502. DefaultSettingsContainer conf;
  1503. CustomSettingsTestType dummy = conf.Read(U_("Test"), U_("struct"), CustomSettingsTestType(1.0f, 1.0f));
  1504. dummy = CustomSettingsTestType(0.125f, 32.0f);
  1505. conf.Write(U_("Test"), U_("struct"), dummy);
  1506. }
  1507. {
  1508. DefaultSettingsContainer conf;
  1509. Setting<CustomSettingsTestType> dummyVar(conf, U_("Test"), U_("struct"), CustomSettingsTestType(1.0f, 1.0f));
  1510. CustomSettingsTestType dummy = dummyVar;
  1511. VERIFY_EQUAL(dummy.x, 0.125f);
  1512. VERIFY_EQUAL(dummy.y, 32.0f);
  1513. }
  1514. #endif // MODPLUG_TRACKER
  1515. }
  1516. // Test MIDI Event generating / reading
  1517. static MPT_NOINLINE void TestMIDIEvents()
  1518. {
  1519. uint32 midiEvent;
  1520. midiEvent = MIDIEvents::CC(MIDIEvents::MIDICC_Balance_Coarse, 13, 40);
  1521. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evControllerChange);
  1522. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), 13);
  1523. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), MIDIEvents::MIDICC_Balance_Coarse);
  1524. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 40);
  1525. midiEvent = MIDIEvents::NoteOn(10, 50, 120);
  1526. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evNoteOn);
  1527. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), 10);
  1528. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), 50);
  1529. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 120);
  1530. midiEvent = MIDIEvents::NoteOff(15, 127, 42);
  1531. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evNoteOff);
  1532. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), 15);
  1533. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), 127);
  1534. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 42);
  1535. midiEvent = MIDIEvents::ProgramChange(1, 127);
  1536. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evProgramChange);
  1537. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), 1);
  1538. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), 127);
  1539. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 0);
  1540. midiEvent = MIDIEvents::PitchBend(2, MIDIEvents::pitchBendCentre);
  1541. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evPitchBend);
  1542. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), 2);
  1543. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), 0x00);
  1544. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 0x40);
  1545. midiEvent = MIDIEvents::System(MIDIEvents::sysStart);
  1546. VERIFY_EQUAL_NONCONT(MIDIEvents::GetTypeFromEvent(midiEvent), MIDIEvents::evSystem);
  1547. VERIFY_EQUAL_NONCONT(MIDIEvents::GetChannelFromEvent(midiEvent), MIDIEvents::sysStart);
  1548. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte1FromEvent(midiEvent), 0);
  1549. VERIFY_EQUAL_NONCONT(MIDIEvents::GetDataByte2FromEvent(midiEvent), 0);
  1550. }
  1551. // Check if our test file was loaded correctly.
  1552. static void TestLoadXMFile(const CSoundFile &sndFile)
  1553. {
  1554. #ifdef MODPLUG_TRACKER
  1555. const CModDoc *pModDoc = sndFile.GetpModDoc();
  1556. VERIFY_EQUAL_NONCONT(pModDoc->IsChannelUnused(0), true);
  1557. VERIFY_EQUAL_NONCONT(pModDoc->IsChannelUnused(1), false);
  1558. #endif // MODPLUG_TRACKER
  1559. // Global Variables
  1560. VERIFY_EQUAL_NONCONT(sndFile.GetTitle(), "Test Module");
  1561. VERIFY_EQUAL_NONCONT(sndFile.m_songMessage.substr(0, 32), "OpenMPT Module Loader Test Suite");
  1562. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultTempo, TEMPO(139, 0));
  1563. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultSpeed, 5);
  1564. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultGlobalVolume, 128);
  1565. VERIFY_EQUAL_NONCONT(sndFile.m_nVSTiVolume, 42);
  1566. VERIFY_EQUAL_NONCONT(sndFile.m_nSamplePreAmp, 23);
  1567. VERIFY_EQUAL_NONCONT((sndFile.m_SongFlags & SONG_FILE_FLAGS), SONG_LINEARSLIDES | SONG_EXFILTERRANGE);
  1568. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[MSF_COMPATIBLE_PLAY], true);
  1569. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMIDICCBugEmulation], false);
  1570. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMPTOldSwingBehaviour], false);
  1571. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kOldMIDIPitchBends], false);
  1572. VERIFY_EQUAL_NONCONT(sndFile.GetMixLevels(), MixLevels::Compatible);
  1573. VERIFY_EQUAL_NONCONT(sndFile.m_nTempoMode, TempoMode::Modern);
  1574. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultRowsPerBeat, 6);
  1575. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultRowsPerMeasure, 12);
  1576. VERIFY_EQUAL_NONCONT(sndFile.m_dwCreatedWithVersion, MPT_V("1.19.02.05"));
  1577. VERIFY_EQUAL_NONCONT(sndFile.Order().GetRestartPos(), 1);
  1578. // Macros
  1579. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetParameteredMacroType(0), kSFxReso);
  1580. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetParameteredMacroType(1), kSFxDryWet);
  1581. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetFixedMacroType(), kZxxResoFltMode);
  1582. // Channels
  1583. VERIFY_EQUAL_NONCONT(sndFile.GetNumChannels(), 2);
  1584. VERIFY_EQUAL_NONCONT((sndFile.ChnSettings[0].szName == "First Channel"), true);
  1585. #ifndef NO_PLUGINS
  1586. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].nMixPlugin, 0);
  1587. #endif // NO_PLUGINS
  1588. VERIFY_EQUAL_NONCONT((sndFile.ChnSettings[1].szName == "Second Channel"), true);
  1589. #ifndef NO_PLUGINS
  1590. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].nMixPlugin, 1);
  1591. #endif // NO_PLUGINS
  1592. // Samples
  1593. VERIFY_EQUAL_NONCONT(sndFile.GetNumSamples(), 3);
  1594. VERIFY_EQUAL_NONCONT((sndFile.m_szNames[1] == "Pulse Sample"), true);
  1595. VERIFY_EQUAL_NONCONT((sndFile.m_szNames[2] == "Empty Sample"), true);
  1596. VERIFY_EQUAL_NONCONT((sndFile.m_szNames[3] == "Unassigned Sample"), true);
  1597. #ifdef MODPLUG_TRACKER
  1598. VERIFY_EQUAL_NONCONT(pModDoc->FindSampleParent(1), 1);
  1599. VERIFY_EQUAL_NONCONT(pModDoc->FindSampleParent(2), 1);
  1600. VERIFY_EQUAL_NONCONT(pModDoc->FindSampleParent(3), INSTRUMENTINDEX_INVALID);
  1601. #endif // MODPLUG_TRACKER
  1602. const ModSample &sample = sndFile.GetSample(1);
  1603. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 1);
  1604. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 1);
  1605. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 1);
  1606. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 16);
  1607. VERIFY_EQUAL_NONCONT(sample.nFineTune, 35);
  1608. VERIFY_EQUAL_NONCONT(sample.RelativeTone, 1);
  1609. VERIFY_EQUAL_NONCONT(sample.nVolume, 32 * 4);
  1610. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 64);
  1611. VERIFY_EQUAL_NONCONT(sample.nPan, 160);
  1612. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_PANNING | CHN_LOOP | CHN_PINGPONGLOOP);
  1613. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 1);
  1614. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 8);
  1615. VERIFY_EQUAL_NONCONT(sample.nVibType, VIB_SQUARE);
  1616. VERIFY_EQUAL_NONCONT(sample.nVibSweep, 3);
  1617. VERIFY_EQUAL_NONCONT(sample.nVibRate, 4);
  1618. VERIFY_EQUAL_NONCONT(sample.nVibDepth, 5);
  1619. // Sample Data
  1620. for(size_t i = 0; i < 6; i++)
  1621. {
  1622. VERIFY_EQUAL_NONCONT(sample.sample8()[i], 18);
  1623. }
  1624. for(size_t i = 6; i < 16; i++)
  1625. {
  1626. VERIFY_EQUAL_NONCONT(sample.sample8()[i], 0);
  1627. }
  1628. // Instruments
  1629. VERIFY_EQUAL_NONCONT(sndFile.GetNumInstruments(), 1);
  1630. const ModInstrument *pIns = sndFile.Instruments[1];
  1631. VERIFY_EQUAL_NONCONT(pIns->nFadeOut, 1024);
  1632. VERIFY_EQUAL_NONCONT(pIns->nPan, 128);
  1633. VERIFY_EQUAL_NONCONT(pIns->dwFlags, InstrumentFlags(0));
  1634. VERIFY_EQUAL_NONCONT(pIns->nPPS, 0);
  1635. VERIFY_EQUAL_NONCONT(pIns->nPPC, NOTE_MIDDLEC - 1);
  1636. VERIFY_EQUAL_NONCONT(pIns->nVolRampUp, 1200);
  1637. VERIFY_EQUAL_NONCONT(pIns->resampling, SRCMODE_SINC8LP);
  1638. VERIFY_EQUAL_NONCONT(pIns->IsCutoffEnabled(), false);
  1639. VERIFY_EQUAL_NONCONT(pIns->GetCutoff(), 0);
  1640. VERIFY_EQUAL_NONCONT(pIns->IsResonanceEnabled(), false);
  1641. VERIFY_EQUAL_NONCONT(pIns->GetResonance(), 0);
  1642. VERIFY_EQUAL_NONCONT(pIns->filterMode, FilterMode::Unchanged);
  1643. VERIFY_EQUAL_NONCONT(pIns->nVolSwing, 0);
  1644. VERIFY_EQUAL_NONCONT(pIns->nPanSwing, 0);
  1645. VERIFY_EQUAL_NONCONT(pIns->nCutSwing, 0);
  1646. VERIFY_EQUAL_NONCONT(pIns->nResSwing, 0);
  1647. VERIFY_EQUAL_NONCONT(pIns->nNNA, NewNoteAction::NoteCut);
  1648. VERIFY_EQUAL_NONCONT(pIns->nDCT, DuplicateCheckType::None);
  1649. VERIFY_EQUAL_NONCONT(pIns->nMixPlug, 1);
  1650. VERIFY_EQUAL_NONCONT(pIns->nMidiChannel, 16);
  1651. VERIFY_EQUAL_NONCONT(pIns->nMidiProgram, 64);
  1652. VERIFY_EQUAL_NONCONT(pIns->wMidiBank, 2);
  1653. VERIFY_EQUAL_NONCONT(pIns->midiPWD, 8);
  1654. VERIFY_EQUAL_NONCONT(pIns->pTuning, sndFile.GetDefaultTuning());
  1655. VERIFY_EQUAL_NONCONT(pIns->pitchToTempoLock, TEMPO(0, 0));
  1656. VERIFY_EQUAL_NONCONT(pIns->pluginVelocityHandling, PLUGIN_VELOCITYHANDLING_VOLUME);
  1657. VERIFY_EQUAL_NONCONT(pIns->pluginVolumeHandling, PLUGIN_VOLUMEHANDLING_MIDI);
  1658. for(size_t i = sndFile.GetModSpecifications().noteMin; i < sndFile.GetModSpecifications().noteMax; i++)
  1659. {
  1660. VERIFY_EQUAL_NONCONT(pIns->Keyboard[i], (i == NOTE_MIDDLEC - 1) ? 2 : 1);
  1661. }
  1662. VERIFY_EQUAL_NONCONT(pIns->VolEnv.dwFlags, ENV_ENABLED | ENV_SUSTAIN);
  1663. VERIFY_EQUAL_NONCONT(pIns->VolEnv.size(), 3);
  1664. VERIFY_EQUAL_NONCONT(pIns->VolEnv.nReleaseNode, ENV_RELEASE_NODE_UNSET);
  1665. VERIFY_EQUAL_NONCONT(pIns->VolEnv[2].tick, 96);
  1666. VERIFY_EQUAL_NONCONT(pIns->VolEnv[2].value, 0);
  1667. VERIFY_EQUAL_NONCONT(pIns->VolEnv.nSustainStart, 1);
  1668. VERIFY_EQUAL_NONCONT(pIns->VolEnv.nSustainEnd, 1);
  1669. VERIFY_EQUAL_NONCONT(pIns->PanEnv.dwFlags, ENV_LOOP);
  1670. VERIFY_EQUAL_NONCONT(pIns->PanEnv.size(), 12);
  1671. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nLoopStart, 9);
  1672. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nLoopEnd, 11);
  1673. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nReleaseNode, ENV_RELEASE_NODE_UNSET);
  1674. VERIFY_EQUAL_NONCONT(pIns->PanEnv[9].tick, 46);
  1675. VERIFY_EQUAL_NONCONT(pIns->PanEnv[9].value, 23);
  1676. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.dwFlags, EnvelopeFlags(0));
  1677. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.size(), 0);
  1678. // Sequences
  1679. VERIFY_EQUAL_NONCONT(sndFile.Order.GetNumSequences(), 1);
  1680. VERIFY_EQUAL_NONCONT(sndFile.Order()[0], 0);
  1681. VERIFY_EQUAL_NONCONT(sndFile.Order()[1], 1);
  1682. // Patterns
  1683. VERIFY_EQUAL_NONCONT(sndFile.Patterns.GetNumPatterns(), 2);
  1684. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetName(), "First Pattern");
  1685. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumRows(), 64);
  1686. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumChannels(), 2);
  1687. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetOverrideSignature(), false);
  1688. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetRowsPerBeat(), 0);
  1689. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetRowsPerMeasure(), 0);
  1690. VERIFY_EQUAL_NONCONT(sndFile.Patterns.IsPatternEmpty(0), true);
  1691. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetName(), "Second Pattern");
  1692. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetNumRows(), 32);
  1693. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetNumChannels(), 2);
  1694. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetOverrideSignature(), false);
  1695. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerBeat(), 0);
  1696. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerMeasure(), 0);
  1697. VERIFY_EQUAL_NONCONT(sndFile.Patterns.IsPatternEmpty(1), false);
  1698. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->IsPcNote(), false);
  1699. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->note, NOTE_NONE);
  1700. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->instr, 0);
  1701. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->volcmd, VOLCMD_VIBRATOSPEED);
  1702. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->vol, 15);
  1703. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 0)->IsEmpty(), true);
  1704. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->IsEmpty(), false);
  1705. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->IsPcNote(), false);
  1706. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->note, NOTE_MIDDLEC + 12);
  1707. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->instr, 45);
  1708. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->volcmd, VOLCMD_VOLSLIDEDOWN);
  1709. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->vol, 5);
  1710. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->command, CMD_PANNING8);
  1711. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->param, 0xFF);
  1712. // Test 4-Bit Panning conversion
  1713. for(int i = 0; i < 16; i++)
  1714. {
  1715. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(10 + i, 0)->vol, i * 4);
  1716. }
  1717. // Plugins
  1718. #ifndef NO_PLUGINS
  1719. const SNDMIXPLUGIN &plug = sndFile.m_MixPlugins[0];
  1720. VERIFY_EQUAL_NONCONT(plug.GetName(), U_("First Plugin"));
  1721. VERIFY_EQUAL_NONCONT(plug.fDryRatio, 0.26f);
  1722. VERIFY_EQUAL_NONCONT(plug.IsMasterEffect(), true);
  1723. VERIFY_EQUAL_NONCONT(plug.GetGain(), 11);
  1724. #endif // NO_PLUGINS
  1725. }
  1726. // Check if our test file was loaded correctly.
  1727. static void TestLoadMPTMFile(const CSoundFile &sndFile)
  1728. {
  1729. // Global Variables
  1730. VERIFY_EQUAL_NONCONT(sndFile.GetTitle(), "Test Module_____________X");
  1731. VERIFY_EQUAL_NONCONT(sndFile.m_songMessage.substr(0, 32), "OpenMPT Module Loader Test Suite");
  1732. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultTempo, TEMPO(139, 999));
  1733. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultSpeed, 5);
  1734. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultGlobalVolume, 128);
  1735. VERIFY_EQUAL_NONCONT(sndFile.m_nVSTiVolume, 42);
  1736. VERIFY_EQUAL_NONCONT(sndFile.m_nSamplePreAmp, 23);
  1737. VERIFY_EQUAL_NONCONT((sndFile.m_SongFlags & SONG_FILE_FLAGS), SONG_LINEARSLIDES | SONG_EXFILTERRANGE | SONG_ITCOMPATGXX | SONG_ITOLDEFFECTS);
  1738. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[MSF_COMPATIBLE_PLAY], true);
  1739. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMIDICCBugEmulation], false);
  1740. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMPTOldSwingBehaviour], false);
  1741. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kOldMIDIPitchBends], false);
  1742. VERIFY_EQUAL_NONCONT(sndFile.GetMixLevels(), MixLevels::Compatible);
  1743. VERIFY_EQUAL_NONCONT(sndFile.m_nTempoMode, TempoMode::Modern);
  1744. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultRowsPerBeat, 6);
  1745. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultRowsPerMeasure, 12);
  1746. VERIFY_EQUAL_NONCONT(sndFile.m_dwCreatedWithVersion, MPT_V("1.19.02.05"));
  1747. VERIFY_EQUAL_NONCONT(sndFile.m_nResampling, SRCMODE_SINC8LP);
  1748. VERIFY_EQUAL_NONCONT(sndFile.m_songArtist, U_("Tester"));
  1749. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing.size(), 6);
  1750. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[0], 29360125);
  1751. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[1], 4194305);
  1752. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[2], 29360128);
  1753. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[3], 4194305);
  1754. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[4], 29360128);
  1755. VERIFY_EQUAL_NONCONT(sndFile.m_tempoSwing[5], 4194305);
  1756. // Edit history
  1757. VERIFY_EQUAL_NONCONT(sndFile.GetFileHistory().size() > 15, true);
  1758. const FileHistory &fh = sndFile.GetFileHistory().front();
  1759. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_year, 111);
  1760. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_mon, 5);
  1761. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_mday, 14);
  1762. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_hour, 21);
  1763. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_min, 8);
  1764. VERIFY_EQUAL_NONCONT(fh.loadDate.tm_sec, 32);
  1765. VERIFY_EQUAL_NONCONT((uint32)((double)fh.openTime / HISTORY_TIMER_PRECISION), 31);
  1766. // Macros
  1767. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetParameteredMacroType(0), kSFxReso);
  1768. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetParameteredMacroType(1), kSFxDryWet);
  1769. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetParameteredMacroType(2), kSFxPolyAT);
  1770. VERIFY_EQUAL_NONCONT(sndFile.m_MidiCfg.GetFixedMacroType(), kZxxResoFltMode);
  1771. // Channels
  1772. VERIFY_EQUAL_NONCONT(sndFile.GetNumChannels(), 70);
  1773. VERIFY_EQUAL_NONCONT((sndFile.ChnSettings[0].szName == "First Channel"), true);
  1774. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].nPan, 32);
  1775. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].nVolume, 32);
  1776. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].dwFlags, CHN_MUTE);
  1777. #ifndef NO_PLUGINS
  1778. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].nMixPlugin, 0);
  1779. #endif // NO_PLUGINS
  1780. VERIFY_EQUAL_NONCONT((sndFile.ChnSettings[1].szName == "Second Channel"), true);
  1781. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].nPan, 128);
  1782. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].nVolume, 16);
  1783. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].dwFlags, CHN_SURROUND);
  1784. #ifndef NO_PLUGINS
  1785. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].nMixPlugin, 1);
  1786. #endif // NO_PLUGINS
  1787. VERIFY_EQUAL_NONCONT((sndFile.ChnSettings[69].szName == "Last Channel______X"), true);
  1788. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[69].nPan, 256);
  1789. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[69].nVolume, 7);
  1790. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[69].dwFlags, ChannelFlags(0));
  1791. #ifndef NO_PLUGINS
  1792. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[69].nMixPlugin, 1);
  1793. #endif // NO_PLUGINS
  1794. // Samples
  1795. VERIFY_EQUAL_NONCONT(sndFile.GetNumSamples(), 4);
  1796. {
  1797. const ModSample &sample = sndFile.GetSample(1);
  1798. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 1);
  1799. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 1);
  1800. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 1);
  1801. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 16);
  1802. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_MPT), 9001);
  1803. VERIFY_EQUAL_NONCONT(sample.nVolume, 32 * 4);
  1804. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 16);
  1805. VERIFY_EQUAL_NONCONT(sample.nPan, 160);
  1806. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_PANNING | CHN_LOOP | CHN_SUSTAINLOOP | CHN_PINGPONGSUSTAIN);
  1807. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 1);
  1808. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 8);
  1809. VERIFY_EQUAL_NONCONT(sample.nSustainStart, 1);
  1810. VERIFY_EQUAL_NONCONT(sample.nSustainEnd, 7);
  1811. VERIFY_EQUAL_NONCONT(sample.nVibType, VIB_SQUARE);
  1812. VERIFY_EQUAL_NONCONT(sample.nVibSweep, 3);
  1813. VERIFY_EQUAL_NONCONT(sample.nVibRate, 4);
  1814. VERIFY_EQUAL_NONCONT(sample.nVibDepth, 5);
  1815. // Sample Data
  1816. for(size_t i = 0; i < 6; i++)
  1817. {
  1818. VERIFY_EQUAL_NONCONT(sample.sample8()[i], 18);
  1819. }
  1820. for(size_t i = 6; i < 16; i++)
  1821. {
  1822. VERIFY_EQUAL_NONCONT(sample.sample8()[i], 0);
  1823. }
  1824. VERIFY_EQUAL_NONCONT(sample.cues[0], 2);
  1825. VERIFY_EQUAL_NONCONT(sample.cues[8], 9);
  1826. }
  1827. {
  1828. const ModSample &sample = sndFile.GetSample(2);
  1829. VERIFY_EQUAL_NONCONT((sndFile.m_szNames[2] == "Stereo / 16-Bit"), true);
  1830. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 4);
  1831. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 2);
  1832. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 2);
  1833. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 16 * 4);
  1834. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_MPT), 16000);
  1835. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_16BIT | CHN_STEREO | CHN_LOOP);
  1836. // Sample Data (Stereo Interleaved)
  1837. for(size_t i = 0; i < 7; i++)
  1838. {
  1839. VERIFY_EQUAL_NONCONT(sample.sample16()[4 + i], int16(-32768));
  1840. }
  1841. VERIFY_EQUAL_NONCONT(sample.cues[0], 3);
  1842. VERIFY_EQUAL_NONCONT(sample.cues[8], 14);
  1843. }
  1844. // External sample
  1845. {
  1846. const ModSample &sample = sndFile.GetSample(4);
  1847. VERIFY_EQUAL_NONCONT((sndFile.m_szNames[4] == "Overridden Name"), true);
  1848. VERIFY_EQUAL_NONCONT((sample.filename == "External"), true);
  1849. #ifdef MPT_EXTERNAL_SAMPLES
  1850. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 1);
  1851. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 1);
  1852. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 1);
  1853. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 64);
  1854. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 42);
  1855. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 55);
  1856. VERIFY_EQUAL_NONCONT(sample.nSustainStart, 42);
  1857. VERIFY_EQUAL_NONCONT(sample.nSustainEnd, 55);
  1858. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_LOOP | CHN_SUSTAINLOOP | CHN_PINGPONGSUSTAIN | SMP_KEEPONDISK);
  1859. #endif // MPT_EXTERNAL_SAMPLES
  1860. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_MPT), 10101);
  1861. VERIFY_EQUAL_NONCONT(sample.nVolume, 26 * 4);
  1862. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 26);
  1863. VERIFY_EQUAL_NONCONT(sample.nPan, 26 * 4);
  1864. VERIFY_EQUAL_NONCONT(sample.nVibType, VIB_SINE);
  1865. VERIFY_EQUAL_NONCONT(sample.nVibSweep, 37);
  1866. VERIFY_EQUAL_NONCONT(sample.nVibRate, 42);
  1867. VERIFY_EQUAL_NONCONT(sample.nVibDepth, 23);
  1868. // Sample Data
  1869. #ifdef MPT_EXTERNAL_SAMPLES
  1870. for(size_t i = 0; i < 16; i++)
  1871. {
  1872. VERIFY_EQUAL_NONCONT(sample.sample8()[i], int8(45));
  1873. }
  1874. #endif // MPT_EXTERNAL_SAMPLES
  1875. VERIFY_EQUAL_NONCONT(sample.cues[0], 10);
  1876. VERIFY_EQUAL_NONCONT(sample.cues[8], 50);
  1877. }
  1878. // Instruments
  1879. VERIFY_EQUAL_NONCONT(sndFile.GetNumInstruments(), 2);
  1880. for(INSTRUMENTINDEX ins = 1; ins <= 2; ins++)
  1881. {
  1882. const ModInstrument *pIns = sndFile.Instruments[ins];
  1883. VERIFY_EQUAL_NONCONT(pIns->nGlobalVol, 32);
  1884. VERIFY_EQUAL_NONCONT(pIns->nFadeOut, 1024);
  1885. VERIFY_EQUAL_NONCONT(pIns->nPan, 64);
  1886. VERIFY_EQUAL_NONCONT(pIns->dwFlags, INS_SETPANNING);
  1887. VERIFY_EQUAL_NONCONT(pIns->nPPS, 8);
  1888. VERIFY_EQUAL_NONCONT(pIns->nPPC, (NOTE_MIDDLEC - NOTE_MIN) + 6); // F#5
  1889. VERIFY_EQUAL_NONCONT(pIns->nVolRampUp, 1200);
  1890. VERIFY_EQUAL_NONCONT(pIns->resampling, SRCMODE_SINC8LP);
  1891. VERIFY_EQUAL_NONCONT(pIns->IsCutoffEnabled(), true);
  1892. VERIFY_EQUAL_NONCONT(pIns->GetCutoff(), 0x32);
  1893. VERIFY_EQUAL_NONCONT(pIns->IsResonanceEnabled(), true);
  1894. VERIFY_EQUAL_NONCONT(pIns->GetResonance(), 0x64);
  1895. VERIFY_EQUAL_NONCONT(pIns->filterMode, FilterMode::HighPass);
  1896. VERIFY_EQUAL_NONCONT(pIns->nVolSwing, 0x30);
  1897. VERIFY_EQUAL_NONCONT(pIns->nPanSwing, 0x18);
  1898. VERIFY_EQUAL_NONCONT(pIns->nCutSwing, 0x0C);
  1899. VERIFY_EQUAL_NONCONT(pIns->nResSwing, 0x3C);
  1900. VERIFY_EQUAL_NONCONT(pIns->nNNA, NewNoteAction::Continue);
  1901. VERIFY_EQUAL_NONCONT(pIns->nDCT, DuplicateCheckType::Note);
  1902. VERIFY_EQUAL_NONCONT(pIns->nDNA, DuplicateNoteAction::NoteFade);
  1903. VERIFY_EQUAL_NONCONT(pIns->nMixPlug, 1);
  1904. VERIFY_EQUAL_NONCONT(pIns->nMidiChannel, 16);
  1905. VERIFY_EQUAL_NONCONT(pIns->nMidiProgram, 64);
  1906. VERIFY_EQUAL_NONCONT(pIns->wMidiBank, 2);
  1907. VERIFY_EQUAL_NONCONT(pIns->midiPWD, ins);
  1908. VERIFY_EQUAL_NONCONT(pIns->pTuning, sndFile.GetDefaultTuning());
  1909. VERIFY_EQUAL_NONCONT(pIns->pitchToTempoLock, TEMPO(130, 2000));
  1910. VERIFY_EQUAL_NONCONT(pIns->pluginVelocityHandling, PLUGIN_VELOCITYHANDLING_VOLUME);
  1911. VERIFY_EQUAL_NONCONT(pIns->pluginVolumeHandling, PLUGIN_VOLUMEHANDLING_MIDI);
  1912. for(size_t i = 0; i < NOTE_MAX; i++)
  1913. {
  1914. VERIFY_EQUAL_NONCONT(pIns->Keyboard[i], (i == NOTE_MIDDLEC - 1) ? 99 : 1);
  1915. VERIFY_EQUAL_NONCONT(pIns->NoteMap[i], (i == NOTE_MIDDLEC - 1) ? (i + 13) : (i + 1));
  1916. }
  1917. VERIFY_EQUAL_NONCONT(pIns->VolEnv.dwFlags, ENV_ENABLED | ENV_CARRY);
  1918. VERIFY_EQUAL_NONCONT(pIns->VolEnv.size(), 3);
  1919. VERIFY_EQUAL_NONCONT(pIns->VolEnv.nReleaseNode, 1);
  1920. VERIFY_EQUAL_NONCONT(pIns->VolEnv[2].tick, 96);
  1921. VERIFY_EQUAL_NONCONT(pIns->VolEnv[2].value, 0);
  1922. VERIFY_EQUAL_NONCONT(pIns->PanEnv.dwFlags, ENV_LOOP);
  1923. VERIFY_EQUAL_NONCONT(pIns->PanEnv.size(), 76);
  1924. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nLoopStart, 22);
  1925. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nLoopEnd, 29);
  1926. VERIFY_EQUAL_NONCONT(pIns->PanEnv.nReleaseNode, ENV_RELEASE_NODE_UNSET);
  1927. VERIFY_EQUAL_NONCONT(pIns->PanEnv[75].tick, 427);
  1928. VERIFY_EQUAL_NONCONT(pIns->PanEnv[75].value, 27);
  1929. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.dwFlags, ENV_ENABLED | ENV_CARRY | ENV_SUSTAIN | ENV_FILTER);
  1930. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.size(), 3);
  1931. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.nSustainStart, 1);
  1932. VERIFY_EQUAL_NONCONT(pIns->PitchEnv.nSustainEnd, 2);
  1933. VERIFY_EQUAL_NONCONT(pIns->PitchEnv[1].tick, 96);
  1934. VERIFY_EQUAL_NONCONT(pIns->PitchEnv[1].value, 64);
  1935. }
  1936. // Sequences
  1937. VERIFY_EQUAL_NONCONT(sndFile.Order.GetNumSequences(), 2);
  1938. VERIFY_EQUAL_NONCONT(sndFile.Order(0).GetLengthTailTrimmed(), 3);
  1939. VERIFY_EQUAL_NONCONT(sndFile.Order(0).GetName(), U_("First Sequence"));
  1940. VERIFY_EQUAL_NONCONT(sndFile.Order(0)[0], sndFile.Order.GetIgnoreIndex());
  1941. VERIFY_EQUAL_NONCONT(sndFile.Order(0)[1], 0);
  1942. VERIFY_EQUAL_NONCONT(sndFile.Order(0)[2], sndFile.Order.GetIgnoreIndex());
  1943. VERIFY_EQUAL_NONCONT(sndFile.Order(0).GetRestartPos(), 1);
  1944. VERIFY_EQUAL_NONCONT(sndFile.Order(1).GetLengthTailTrimmed(), 3);
  1945. VERIFY_EQUAL_NONCONT(sndFile.Order(1).GetName(), U_("Second Sequence"));
  1946. VERIFY_EQUAL_NONCONT(sndFile.Order(1)[0], 1);
  1947. VERIFY_EQUAL_NONCONT(sndFile.Order(1)[1], 2);
  1948. VERIFY_EQUAL_NONCONT(sndFile.Order(1)[2], 3);
  1949. VERIFY_EQUAL_NONCONT(sndFile.Order(1).GetRestartPos(), 2);
  1950. // Patterns
  1951. VERIFY_EQUAL_NONCONT(sndFile.Patterns.GetNumPatterns(), 2);
  1952. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetName(), "First Pattern");
  1953. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumRows(), 70);
  1954. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumChannels(), 70);
  1955. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetOverrideSignature(), true);
  1956. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetRowsPerBeat(), 5);
  1957. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetRowsPerMeasure(), 10);
  1958. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].HasTempoSwing(), true);
  1959. VERIFY_EQUAL_NONCONT(sndFile.Patterns.IsPatternEmpty(0), true);
  1960. {
  1961. TempoSwing swing = sndFile.Patterns[0].GetTempoSwing();
  1962. VERIFY_EQUAL_NONCONT(swing.size(), 5);
  1963. VERIFY_EQUAL_NONCONT(swing[0], 16770149);
  1964. VERIFY_EQUAL_NONCONT(swing[1], 16803696);
  1965. VERIFY_EQUAL_NONCONT(swing[2], 16770157);
  1966. VERIFY_EQUAL_NONCONT(swing[3], 29347774);
  1967. VERIFY_EQUAL_NONCONT(swing[4], 4194304);
  1968. }
  1969. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetName(), "Second Pattern");
  1970. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetNumRows(), 32);
  1971. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetNumChannels(), 70);
  1972. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetOverrideSignature(), false);
  1973. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerBeat(), 0);
  1974. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerMeasure(), 0);
  1975. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].HasTempoSwing(), false);
  1976. VERIFY_EQUAL_NONCONT(sndFile.Patterns.IsPatternEmpty(1), false);
  1977. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->IsPcNote(), true);
  1978. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->note, NOTE_PC);
  1979. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->instr, 99);
  1980. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->GetValueVolCol(), 1);
  1981. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(0, 0)->GetValueEffectCol(), 200);
  1982. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 0)->IsEmpty(), true);
  1983. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->IsEmpty(), false);
  1984. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->IsPcNote(), false);
  1985. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->note, NOTE_MIDDLEC + 12);
  1986. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->instr, 45);
  1987. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->volcmd, VOLCMD_VOLSLIDEDOWN);
  1988. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->vol, 5);
  1989. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->command, CMD_PANNING8);
  1990. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(31, 1)->param, 0xFF);
  1991. // Plugins
  1992. #ifndef NO_PLUGINS
  1993. const SNDMIXPLUGIN &plug = sndFile.m_MixPlugins[0];
  1994. VERIFY_EQUAL_NONCONT(plug.GetName(), U_("First Plugin"));
  1995. VERIFY_EQUAL_NONCONT(plug.fDryRatio, 0.26f);
  1996. VERIFY_EQUAL_NONCONT(plug.IsMasterEffect(), true);
  1997. VERIFY_EQUAL_NONCONT(plug.GetGain(), 11);
  1998. VERIFY_EQUAL_NONCONT(plug.pMixPlugin != nullptr, true);
  1999. if(plug.pMixPlugin)
  2000. {
  2001. VERIFY_EQUAL_NONCONT(plug.pMixPlugin->GetParameter(1), 0.5f);
  2002. VERIFY_EQUAL_NONCONT(plug.pMixPlugin->IsInstrument(), false);
  2003. }
  2004. #endif // NO_PLUGINS
  2005. #ifdef MODPLUG_TRACKER
  2006. // MIDI Mapping
  2007. VERIFY_EQUAL_NONCONT(sndFile.GetMIDIMapper().GetCount(), 1);
  2008. const CMIDIMappingDirective &mapping = sndFile.GetMIDIMapper().GetDirective(0);
  2009. VERIFY_EQUAL_NONCONT(mapping.GetAllowPatternEdit(), true);
  2010. VERIFY_EQUAL_NONCONT(mapping.GetCaptureMIDI(), false);
  2011. VERIFY_EQUAL_NONCONT(mapping.IsActive(), true);
  2012. VERIFY_EQUAL_NONCONT(mapping.GetAnyChannel(), false);
  2013. VERIFY_EQUAL_NONCONT(mapping.GetChannel(), 5);
  2014. VERIFY_EQUAL_NONCONT(mapping.GetPlugIndex(), 1);
  2015. VERIFY_EQUAL_NONCONT(mapping.GetParamIndex(), 0);
  2016. VERIFY_EQUAL_NONCONT(mapping.GetEvent(), MIDIEvents::evControllerChange);
  2017. VERIFY_EQUAL_NONCONT(mapping.GetController(), MIDIEvents::MIDICC_ModulationWheel_Coarse);
  2018. // Channel colors
  2019. const auto &chns = sndFile.ChnSettings;
  2020. VERIFY_EQUAL_NONCONT(chns[0].color, RGB(255, 0, 0));
  2021. VERIFY_EQUAL_NONCONT(chns[1].color, RGB(0, 255, 0));
  2022. VERIFY_EQUAL_NONCONT(chns[2].color, RGB(0, 0, 255));
  2023. VERIFY_EQUAL_NONCONT(chns[3].color, ModChannelSettings::INVALID_COLOR);
  2024. VERIFY_EQUAL_NONCONT(chns[67].color, RGB(255, 0, 255));
  2025. VERIFY_EQUAL_NONCONT(chns[68].color, RGB(255, 255, 0));
  2026. VERIFY_EQUAL_NONCONT(chns[69].color, ModChannelSettings::INVALID_COLOR);
  2027. #endif
  2028. VERIFY_EQUAL_NONCONT(sndFile.FrequencyToCutOff(sndFile.CutOffToFrequency(0)), 0);
  2029. VERIFY_EQUAL_NONCONT(sndFile.FrequencyToCutOff(sndFile.CutOffToFrequency(80)), 80);
  2030. VERIFY_EQUAL_NONCONT(sndFile.FrequencyToCutOff(sndFile.CutOffToFrequency(127)), 127);
  2031. }
  2032. // Check if our test file was loaded correctly.
  2033. static void TestLoadS3MFile(const CSoundFile &sndFile, bool resaved)
  2034. {
  2035. // Global Variables
  2036. VERIFY_EQUAL_NONCONT(sndFile.GetTitle(), "S3M_Test__________________X");
  2037. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultTempo, TEMPO(33, 0));
  2038. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultSpeed, 254);
  2039. VERIFY_EQUAL_NONCONT(sndFile.m_nDefaultGlobalVolume, 32 * 4);
  2040. VERIFY_EQUAL_NONCONT(sndFile.m_nVSTiVolume, 36);
  2041. VERIFY_EQUAL_NONCONT(sndFile.m_nSamplePreAmp, 16);
  2042. VERIFY_EQUAL_NONCONT((sndFile.m_SongFlags & SONG_FILE_FLAGS), SONG_FASTVOLSLIDES);
  2043. VERIFY_EQUAL_NONCONT(sndFile.GetMixLevels(), MixLevels::Compatible);
  2044. VERIFY_EQUAL_NONCONT(sndFile.m_nTempoMode, TempoMode::Classic);
  2045. VERIFY_EQUAL_NONCONT(sndFile.m_dwLastSavedWithVersion, resaved ? Version::Current() : MPT_V("1.27.00.00"));
  2046. // Channels
  2047. VERIFY_EQUAL_NONCONT(sndFile.GetNumChannels(), 4);
  2048. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].nPan, 0);
  2049. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[0].dwFlags, ChannelFlags(0));
  2050. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].nPan, 256);
  2051. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[1].dwFlags, CHN_MUTE);
  2052. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[2].nPan, 85);
  2053. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[2].dwFlags, ChannelFlags(0));
  2054. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[3].nPan, 171);
  2055. VERIFY_EQUAL_NONCONT(sndFile.ChnSettings[3].dwFlags, CHN_MUTE);
  2056. // Samples
  2057. VERIFY_EQUAL_NONCONT(sndFile.GetNumSamples(), 4);
  2058. {
  2059. const ModSample &sample = sndFile.GetSample(1);
  2060. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[1], "Sample_1__________________X");
  2061. VERIFY_EQUAL_NONCONT(sample.filename, "Filename_1_X");
  2062. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 1);
  2063. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 1);
  2064. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 1);
  2065. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 60);
  2066. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_S3M), 9001);
  2067. VERIFY_EQUAL_NONCONT(sample.nVolume, 32 * 4);
  2068. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 64);
  2069. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_LOOP);
  2070. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 16);
  2071. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 60);
  2072. // Sample Data
  2073. for(size_t i = 0; i < 30; i++)
  2074. {
  2075. VERIFY_EQUAL_NONCONT(sample.sample8()[i], 127);
  2076. }
  2077. for(size_t i = 31; i < 60; i++)
  2078. {
  2079. VERIFY_EQUAL_NONCONT(sample.sample8()[i], -128);
  2080. }
  2081. }
  2082. {
  2083. const ModSample &sample = sndFile.GetSample(2);
  2084. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[2], "Empty");
  2085. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_S3M), 16384);
  2086. VERIFY_EQUAL_NONCONT(sample.nVolume, 2 * 4);
  2087. }
  2088. {
  2089. const ModSample &sample = sndFile.GetSample(3);
  2090. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[3], "Stereo / 16-Bit");
  2091. VERIFY_EQUAL_NONCONT(sample.filename, "Filename_3_X");
  2092. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 4);
  2093. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 2);
  2094. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 2);
  2095. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 64);
  2096. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_S3M), 16000);
  2097. VERIFY_EQUAL_NONCONT(sample.nVolume, 0);
  2098. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_LOOP | CHN_16BIT | CHN_STEREO);
  2099. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 0);
  2100. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 16);
  2101. // Sample Data (Stereo Interleaved)
  2102. for(size_t i = 0; i < 7; i++)
  2103. {
  2104. VERIFY_EQUAL_NONCONT(sample.sample16()[4 + i], int16(-32768));
  2105. }
  2106. }
  2107. {
  2108. const ModSample &sample = sndFile.GetSample(4);
  2109. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[4], "adlib");
  2110. VERIFY_EQUAL_NONCONT((sample.filename == ""), true);
  2111. VERIFY_EQUAL_NONCONT(sample.GetSampleRate(MOD_TYPE_S3M), 8363);
  2112. VERIFY_EQUAL_NONCONT(sample.nVolume, 58 * 4);
  2113. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_ADLIB);
  2114. VERIFY_EQUAL_NONCONT(sample.adlib, (OPLPatch{ { 0x00, 0x00, 0xC0, 0x00, 0xF0, 0xD2, 0x05, 0xB3, 0x01, 0x00, 0x00, 0x00 } }));
  2115. }
  2116. // Orders
  2117. VERIFY_EQUAL_NONCONT(sndFile.Order().GetRestartPos(), 0);
  2118. VERIFY_EQUAL_NONCONT(sndFile.Order().GetLengthTailTrimmed(), 5);
  2119. VERIFY_EQUAL_NONCONT(sndFile.Order()[0], 0);
  2120. VERIFY_EQUAL_NONCONT(sndFile.Order()[1], sndFile.Order.GetIgnoreIndex());
  2121. VERIFY_EQUAL_NONCONT(sndFile.Order()[2], sndFile.Order.GetInvalidPatIndex());
  2122. VERIFY_EQUAL_NONCONT(sndFile.Order()[3], 1);
  2123. VERIFY_EQUAL_NONCONT(sndFile.Order()[4], 0);
  2124. // Patterns
  2125. VERIFY_EQUAL_NONCONT(sndFile.Patterns.GetNumPatterns(), 2);
  2126. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumRows(), 64);
  2127. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetNumChannels(), 4);
  2128. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetOverrideSignature(), false);
  2129. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(0, 0)->note, NOTE_MIN + 12);
  2130. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(1, 0)->note, NOTE_MIN + 107);
  2131. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(0, 1)->volcmd, VOLCMD_VOLUME);
  2132. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(0, 1)->vol, 0);
  2133. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(1, 1)->volcmd, VOLCMD_VOLUME);
  2134. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(1, 1)->vol, 64);
  2135. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(2, 1)->volcmd, VOLCMD_PANNING);
  2136. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(2, 1)->vol, 0);
  2137. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(3, 1)->volcmd, VOLCMD_PANNING);
  2138. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(3, 1)->vol, 64);
  2139. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(0, 3)->command, CMD_SPEED);
  2140. VERIFY_EQUAL_NONCONT(sndFile.Patterns[0].GetpModCommand(0, 3)->param, 0x11);
  2141. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetNumRows(), 64);
  2142. VERIFY_EQUAL_NONCONT(sndFile.Patterns.IsPatternEmpty(1), false);
  2143. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(63, 3)->param, 0x04);
  2144. }
  2145. // Check if our test file was loaded correctly.
  2146. static void TestLoadMODFile(CSoundFile &sndFile)
  2147. {
  2148. // Global Variables
  2149. VERIFY_EQUAL_NONCONT(sndFile.GetTitle(), "MOD_Test___________X");
  2150. VERIFY_EQUAL_NONCONT((sndFile.m_SongFlags & SONG_FILE_FLAGS), SONG_PT_MODE | SONG_AMIGALIMITS | SONG_ISAMIGA);
  2151. VERIFY_EQUAL_NONCONT(sndFile.GetMixLevels(), MixLevels::Compatible);
  2152. VERIFY_EQUAL_NONCONT(sndFile.m_nTempoMode, TempoMode::Classic);
  2153. VERIFY_EQUAL_NONCONT(sndFile.GetNumChannels(), 4);
  2154. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMODOneShotLoops], true);
  2155. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMODSampleSwap], true);
  2156. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMODIgnorePanning], true);
  2157. VERIFY_EQUAL_NONCONT(sndFile.m_playBehaviour[kMODVBlankTiming], false);
  2158. // Test GetLength code, in particular with subsongs
  2159. VERIFY_EQUAL_NONCONT(sndFile.GetLength(eNoAdjust, GetLengthTarget(0, 4)).back().targetReached, false);
  2160. const auto allSubSongs = sndFile.GetLength(eNoAdjust, GetLengthTarget(true));
  2161. VERIFY_EQUAL_NONCONT(allSubSongs.size(), 2);
  2162. VERIFY_EQUAL_EPS(allSubSongs[0].duration, 2.04, 0.1);
  2163. VERIFY_EQUAL_EPS(allSubSongs[1].duration, 118.84, 0.1);
  2164. VERIFY_EQUAL_NONCONT(allSubSongs[0].lastOrder, 0);
  2165. VERIFY_EQUAL_NONCONT(allSubSongs[0].lastRow, 1);
  2166. VERIFY_EQUAL_NONCONT(allSubSongs[1].lastOrder, 2);
  2167. VERIFY_EQUAL_NONCONT(allSubSongs[1].lastRow, 61);
  2168. VERIFY_EQUAL_NONCONT(allSubSongs[1].startOrder, 2);
  2169. VERIFY_EQUAL_NONCONT(allSubSongs[1].startRow, 0);
  2170. // Samples
  2171. VERIFY_EQUAL_NONCONT(sndFile.GetNumSamples(), 31);
  2172. {
  2173. const ModSample &sample = sndFile.GetSample(1);
  2174. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[1], "Sample_1_____________X");
  2175. VERIFY_EQUAL_NONCONT(sample.GetBytesPerSample(), 1);
  2176. VERIFY_EQUAL_NONCONT(sample.GetNumChannels(), 1);
  2177. VERIFY_EQUAL_NONCONT(sample.GetElementarySampleSize(), 1);
  2178. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 1244);
  2179. VERIFY_EQUAL_NONCONT(sample.nFineTune, 0x70);
  2180. VERIFY_EQUAL_NONCONT(sample.RelativeTone, 0);
  2181. VERIFY_EQUAL_NONCONT(sample.nVolume, 256);
  2182. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 64);
  2183. VERIFY_EQUAL_NONCONT(sample.uFlags, CHN_LOOP);
  2184. VERIFY_EQUAL_NONCONT(sample.nLoopStart, 0);
  2185. VERIFY_EQUAL_NONCONT(sample.nLoopEnd, 128);
  2186. // Sample Data
  2187. VERIFY_EQUAL_NONCONT(sample.sample8()[0], 0);
  2188. VERIFY_EQUAL_NONCONT(sample.sample8()[1], 0);
  2189. VERIFY_EQUAL_NONCONT(sample.sample8()[2], -29);
  2190. }
  2191. {
  2192. const ModSample &sample = sndFile.GetSample(3);
  2193. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[3], "OpenMPT Module Loader");
  2194. VERIFY_EQUAL_NONCONT(sample.GetSampleSizeInBytes(), 0);
  2195. VERIFY_EQUAL_NONCONT(sample.nFineTune, -0x80);
  2196. VERIFY_EQUAL_NONCONT(sample.RelativeTone, 0);
  2197. VERIFY_EQUAL_NONCONT(sample.nVolume, 4);
  2198. VERIFY_EQUAL_NONCONT(sample.nGlobalVol, 64);
  2199. }
  2200. // Orders
  2201. VERIFY_EQUAL_NONCONT(sndFile.Order().GetRestartPos(), 0);
  2202. VERIFY_EQUAL_NONCONT(sndFile.Order().GetLengthTailTrimmed(), 4);
  2203. VERIFY_EQUAL_NONCONT(sndFile.Order()[0], 0);
  2204. VERIFY_EQUAL_NONCONT(sndFile.Order()[1], 1);
  2205. VERIFY_EQUAL_NONCONT(sndFile.Order()[2], 2);
  2206. VERIFY_EQUAL_NONCONT(sndFile.Order()[3], 0);
  2207. // Patterns
  2208. VERIFY_EQUAL_NONCONT(sndFile.Patterns.GetNumPatterns(), 3);
  2209. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetNumRows(), 64);
  2210. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(1, 0)->note, NOTE_MIDDLEC + 12);
  2211. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(16, 3)->instr, 1);
  2212. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(19, 3)->command, CMD_PANNING8);
  2213. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(19, 3)->param, 0x28);
  2214. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(20, 0)->command, CMD_TEMPO);
  2215. VERIFY_EQUAL_NONCONT(sndFile.Patterns[2].GetpModCommand(20, 1)->command, CMD_SPEED);
  2216. }
  2217. #ifdef MODPLUG_TRACKER
  2218. static bool ShouldRunTests()
  2219. {
  2220. mpt::PathString theFile = theApp.GetInstallPath();
  2221. if(theFile.IsDirectory() && (theFile + P_("test")).IsDirectory())
  2222. {
  2223. if((theFile + P_("test\\test.mptm")).IsFile())
  2224. {
  2225. return true;
  2226. }
  2227. }
  2228. return false;
  2229. }
  2230. static mpt::PathString GetTestFilenameBase()
  2231. {
  2232. mpt::PathString theFile = theApp.GetInstallPath();
  2233. theFile += P_("test/test.");
  2234. return theFile;
  2235. }
  2236. static mpt::PathString GetTempFilenameBase()
  2237. {
  2238. return GetTestFilenameBase();
  2239. }
  2240. typedef CModDoc *TSoundFileContainer;
  2241. static CSoundFile &GetSoundFile(TSoundFileContainer &sndFile)
  2242. {
  2243. return sndFile->GetSoundFile();
  2244. }
  2245. static TSoundFileContainer CreateSoundFileContainer(const mpt::PathString &filename)
  2246. {
  2247. CModDoc *pModDoc = static_cast<CModDoc *>(theApp.OpenDocumentFile(filename.ToCString(), FALSE));
  2248. return pModDoc;
  2249. }
  2250. static void DestroySoundFileContainer(TSoundFileContainer &sndFile)
  2251. {
  2252. sndFile->OnCloseDocument();
  2253. }
  2254. static void SaveTestFile(const TSoundFileContainer &sndFile, const mpt::PathString &filename)
  2255. {
  2256. sndFile->DoSave(filename);
  2257. // Saving the file puts it in the MRU list...
  2258. theApp.RemoveMruItem(0);
  2259. }
  2260. const auto SaveIT = SaveTestFile;
  2261. const auto SaveXM = SaveTestFile;
  2262. const auto SaveS3M = SaveTestFile;
  2263. const auto SaveMOD = SaveTestFile;
  2264. #else // !MODPLUG_TRACKER
  2265. static bool ShouldRunTests()
  2266. {
  2267. #if MPT_TEST_HAS_FILESYSTEM
  2268. return true;
  2269. #else
  2270. return false;
  2271. #endif
  2272. }
  2273. static mpt::PathString GetTestFilenameBase()
  2274. {
  2275. return Test::GetPathPrefix() + P_("./test/test.");
  2276. }
  2277. static mpt::PathString GetTempFilenameBase()
  2278. {
  2279. return P_("./test.");
  2280. }
  2281. typedef std::shared_ptr<CSoundFile> TSoundFileContainer;
  2282. static CSoundFile &GetSoundFile(TSoundFileContainer &sndFile)
  2283. {
  2284. return *sndFile.get();
  2285. }
  2286. static TSoundFileContainer CreateSoundFileContainer(const mpt::PathString &filename)
  2287. {
  2288. mpt::ifstream stream(filename, std::ios::binary);
  2289. FileReader file = mpt::IO::make_FileCursor<mpt::PathString>(stream);
  2290. std::shared_ptr<CSoundFile> pSndFile = std::make_shared<CSoundFile>();
  2291. pSndFile->Create(file, CSoundFile::loadCompleteModule);
  2292. return pSndFile;
  2293. }
  2294. static void DestroySoundFileContainer(TSoundFileContainer & /* sndFile */ )
  2295. {
  2296. return;
  2297. }
  2298. #ifndef MODPLUG_NO_FILESAVE
  2299. static void SaveIT(const TSoundFileContainer &sndFile, const mpt::PathString &filename)
  2300. {
  2301. mpt::ofstream f(filename, std::ios::binary);
  2302. sndFile->SaveIT(f, filename, false);
  2303. }
  2304. static void SaveXM(const TSoundFileContainer &sndFile, const mpt::PathString &filename)
  2305. {
  2306. mpt::ofstream f(filename, std::ios::binary);
  2307. sndFile->SaveXM(f, false);
  2308. }
  2309. static void SaveS3M(const TSoundFileContainer &sndFile, const mpt::PathString &filename)
  2310. {
  2311. mpt::ofstream f(filename, std::ios::binary);
  2312. sndFile->SaveS3M(f);
  2313. }
  2314. static void SaveMOD(const TSoundFileContainer &sndFile, const mpt::PathString &filename)
  2315. {
  2316. mpt::ofstream f(filename, std::ios::binary);
  2317. sndFile->SaveMod(f);
  2318. }
  2319. #endif // !MODPLUG_NO_FILESAVE
  2320. #endif // MODPLUG_TRACKER
  2321. // Test file loading and saving
  2322. static MPT_NOINLINE void TestLoadSaveFile()
  2323. {
  2324. if(!ShouldRunTests())
  2325. {
  2326. return;
  2327. }
  2328. #ifdef MODPLUG_TRACKER
  2329. bool saveMutedChannels = TrackerSettings::Instance().MiscSaveChannelMuteStatus;
  2330. TrackerSettings::Instance().MiscSaveChannelMuteStatus = true;
  2331. #endif
  2332. mpt::PathString filenameBaseSrc = GetTestFilenameBase();
  2333. mpt::PathString filenameBase = GetTempFilenameBase();
  2334. // Test MPTM file loading
  2335. {
  2336. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBaseSrc + P_("mptm"));
  2337. TestLoadMPTMFile(GetSoundFile(sndFileContainer));
  2338. #ifndef MODPLUG_NO_FILESAVE
  2339. // Test file saving
  2340. GetSoundFile(sndFileContainer).m_dwLastSavedWithVersion = Version::Current();
  2341. SaveIT(sndFileContainer, filenameBase + P_("saved.mptm"));
  2342. #endif
  2343. DestroySoundFileContainer(sndFileContainer);
  2344. }
  2345. // Reload the saved file and test if everything is still working correctly.
  2346. #ifndef MODPLUG_NO_FILESAVE
  2347. {
  2348. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBase + P_("saved.mptm"));
  2349. TestLoadMPTMFile(GetSoundFile(sndFileContainer));
  2350. DestroySoundFileContainer(sndFileContainer);
  2351. RemoveFile(filenameBase + P_("saved.mptm"));
  2352. }
  2353. #endif
  2354. // Test XM file loading
  2355. {
  2356. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBaseSrc + P_("xm"));
  2357. TestLoadXMFile(GetSoundFile(sndFileContainer));
  2358. // In OpenMPT 1.20 (up to revision 1459), there was a bug in the XM saver
  2359. // that would create broken XMs if the sample map contained samples that
  2360. // were only referenced below C-1 or above B-8 (such samples should not
  2361. // be written). Let's insert a sample there and check if re-loading the
  2362. // file still works.
  2363. GetSoundFile(sndFileContainer).m_nSamples++;
  2364. GetSoundFile(sndFileContainer).Instruments[1]->Keyboard[110] = GetSoundFile(sndFileContainer).GetNumSamples();
  2365. #ifndef MODPLUG_NO_FILESAVE
  2366. // Test file saving
  2367. GetSoundFile(sndFileContainer).m_dwLastSavedWithVersion = Version::Current();
  2368. SaveXM(sndFileContainer, filenameBase + P_("saved.xm"));
  2369. #endif
  2370. DestroySoundFileContainer(sndFileContainer);
  2371. }
  2372. // Reload the saved file and test if everything is still working correctly.
  2373. #ifndef MODPLUG_NO_FILESAVE
  2374. {
  2375. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBase + P_("saved.xm"));
  2376. TestLoadXMFile(GetSoundFile(sndFileContainer));
  2377. DestroySoundFileContainer(sndFileContainer);
  2378. RemoveFile(filenameBase + P_("saved.xm"));
  2379. }
  2380. #endif
  2381. // Test S3M file loading
  2382. {
  2383. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBaseSrc + P_("s3m"));
  2384. auto &sndFile = GetSoundFile(sndFileContainer);
  2385. TestLoadS3MFile(sndFile, false);
  2386. // Test GetLength code, in particular with subsongs
  2387. sndFile.ChnSettings[1].dwFlags.reset(CHN_MUTE);
  2388. VERIFY_EQUAL_EPS(sndFile.GetLength(eAdjustSamplePositions, GetLengthTarget(3, 1)).back().duration, 19.237, 0.01);
  2389. VERIFY_EQUAL_NONCONT(sndFile.GetLength(eAdjustSamplePositions, GetLengthTarget(2, 0).StartPos(0, 1, 0)).back().targetReached, false);
  2390. auto allSubSongs = sndFile.GetLength(eNoAdjust, GetLengthTarget(true));
  2391. VERIFY_EQUAL_NONCONT(allSubSongs.size(), 3);
  2392. double totalDuration = 0.0;
  2393. for(const auto &subSong : allSubSongs)
  2394. {
  2395. totalDuration += subSong.duration;
  2396. }
  2397. VERIFY_EQUAL_EPS(totalDuration, 3674.38, 1.0);
  2398. #ifndef MODPLUG_NO_FILESAVE
  2399. // Test file saving
  2400. sndFile.ChnSettings[1].dwFlags.set(CHN_MUTE);
  2401. sndFile.m_dwLastSavedWithVersion = Version::Current();
  2402. SaveS3M(sndFileContainer, filenameBase + P_("saved.s3m"));
  2403. #endif
  2404. DestroySoundFileContainer(sndFileContainer);
  2405. }
  2406. // Reload the saved file and test if everything is still working correctly.
  2407. #ifndef MODPLUG_NO_FILESAVE
  2408. {
  2409. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBase + P_("saved.s3m"));
  2410. TestLoadS3MFile(GetSoundFile(sndFileContainer), true);
  2411. DestroySoundFileContainer(sndFileContainer);
  2412. RemoveFile(filenameBase + P_("saved.s3m"));
  2413. }
  2414. #endif
  2415. // Test MOD file loading
  2416. {
  2417. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBaseSrc + P_("mod"));
  2418. auto &sndFile = GetSoundFile(sndFileContainer);
  2419. TestLoadMODFile(sndFile);
  2420. #ifndef MODPLUG_NO_FILESAVE
  2421. // Test file saving
  2422. SaveMOD(sndFileContainer, filenameBase + P_("saved.mod"));
  2423. #endif
  2424. DestroySoundFileContainer(sndFileContainer);
  2425. }
  2426. // Reload the saved file and test if everything is still working correctly.
  2427. #ifndef MODPLUG_NO_FILESAVE
  2428. {
  2429. TSoundFileContainer sndFileContainer = CreateSoundFileContainer(filenameBase + P_("saved.mod"));
  2430. TestLoadMODFile(GetSoundFile(sndFileContainer));
  2431. DestroySoundFileContainer(sndFileContainer);
  2432. RemoveFile(filenameBase + P_("saved.mod"));
  2433. }
  2434. #endif
  2435. // General file I/O tests
  2436. {
  2437. std::ostringstream f;
  2438. size_t bytesWritten;
  2439. mpt::IO::WriteVarInt(f, uint16(0), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 1);
  2440. mpt::IO::WriteVarInt(f, uint16(127), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 1);
  2441. mpt::IO::WriteVarInt(f, uint16(128), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 2);
  2442. mpt::IO::WriteVarInt(f, uint16(16383), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 2);
  2443. mpt::IO::WriteVarInt(f, uint16(16384), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 3);
  2444. mpt::IO::WriteVarInt(f, uint16(65535), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 3);
  2445. mpt::IO::WriteVarInt(f, uint64(0xFFFFFFFFFFFFFFFFull), &bytesWritten); VERIFY_EQUAL_NONCONT(bytesWritten, 10);
  2446. std::string data = f.str();
  2447. FileReader file(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(data)));
  2448. uint64 v;
  2449. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 0);
  2450. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 127);
  2451. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 128);
  2452. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 16383);
  2453. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 16384);
  2454. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 65535);
  2455. file.ReadVarInt(v); VERIFY_EQUAL_NONCONT(v, 0xFFFFFFFFFFFFFFFFull);
  2456. }
  2457. #ifdef MODPLUG_TRACKER
  2458. TrackerSettings::Instance().MiscSaveChannelMuteStatus = saveMutedChannels;
  2459. #endif
  2460. }
  2461. // Test various editing features
  2462. static MPT_NOINLINE void TestEditing()
  2463. {
  2464. #ifdef MODPLUG_TRACKER
  2465. auto modDoc = static_cast<CModDoc *>(theApp.GetModDocTemplate()->CreateNewDocument());
  2466. auto &sndFile = modDoc->GetSoundFile();
  2467. sndFile.Create(FileReader(), CSoundFile::loadCompleteModule, modDoc);
  2468. sndFile.m_nChannels = 4;
  2469. sndFile.ChangeModTypeTo(MOD_TYPE_MPT);
  2470. // Rearrange channels
  2471. sndFile.Patterns.ResizeArray(2);
  2472. sndFile.Patterns.Insert(0, 32);
  2473. sndFile.Patterns.Insert(1, 48);
  2474. sndFile.Patterns[1].SetName("Pattern");
  2475. sndFile.Patterns[1].SetSignature(2, 4);
  2476. TempoSwing swing;
  2477. swing.resize(2);
  2478. sndFile.Patterns[1].SetTempoSwing(swing);
  2479. sndFile.Patterns[1].GetpModCommand(37, 0)->instr = 1;
  2480. sndFile.Patterns[1].GetpModCommand(37, 1)->instr = 2;
  2481. sndFile.Patterns[1].GetpModCommand(37, 2)->instr = 3;
  2482. sndFile.Patterns[1].GetpModCommand(37, 3)->instr = 4;
  2483. modDoc->ReArrangeChannels({ 3, 2, CHANNELINDEX_INVALID, 0 });
  2484. modDoc->ReArrangeChannels({ 0, 1, 1, CHANNELINDEX_INVALID, 3 });
  2485. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetName(), "Pattern");
  2486. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerBeat(), 2);
  2487. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetRowsPerMeasure(), 4);
  2488. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetTempoSwing(), swing);
  2489. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 0)->instr, 4);
  2490. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 1)->instr, 3);
  2491. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 2)->instr, 3);
  2492. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 3)->instr, 0);
  2493. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 4)->instr, 1);
  2494. // Rearrange samples
  2495. sndFile.m_nSamples = 2;
  2496. sndFile.GetSample(1).filename = "1";
  2497. sndFile.m_szNames[1] = "1";
  2498. sndFile.GetSample(2).filename = "2";
  2499. sndFile.m_szNames[2] = "2";
  2500. sndFile.GetSample(2).nLength = 16;
  2501. sndFile.GetSample(2).AllocateSample();
  2502. modDoc->ReArrangeSamples({ 2, SAMPLEINDEX_INVALID, 1 });
  2503. VERIFY_EQUAL_NONCONT(sndFile.GetSample(1).HasSampleData(), true);
  2504. VERIFY_EQUAL_NONCONT(sndFile.GetSample(1).filename, "2");
  2505. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[1], "2");
  2506. VERIFY_EQUAL_NONCONT(sndFile.GetSample(2).filename, "");
  2507. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[2], "");
  2508. VERIFY_EQUAL_NONCONT(sndFile.GetSample(3).filename, "1");
  2509. VERIFY_EQUAL_NONCONT(sndFile.m_szNames[3], "1");
  2510. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 4)->instr, 3);
  2511. // Convert / rearrange instruments
  2512. modDoc->ConvertSamplesToInstruments();
  2513. modDoc->ReArrangeInstruments({ INSTRUMENTINDEX_INVALID, 2, 1, 3 });
  2514. VERIFY_EQUAL_NONCONT(sndFile.Instruments[1]->name, "");
  2515. VERIFY_EQUAL_NONCONT(sndFile.Instruments[2]->name, "");
  2516. VERIFY_EQUAL_NONCONT(sndFile.Instruments[3]->name, "2");
  2517. VERIFY_EQUAL_NONCONT(sndFile.Instruments[4]->name, "1");
  2518. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 4)->instr, 4);
  2519. modDoc->ConvertInstrumentsToSamples();
  2520. VERIFY_EQUAL_NONCONT(sndFile.Patterns[1].GetpModCommand(37, 4)->instr, 3);
  2521. modDoc->SetModified();
  2522. VERIFY_EQUAL_NONCONT(modDoc->IsModified(), true);
  2523. VERIFY_EQUAL_NONCONT(modDoc->ModifiedSinceLastAutosave(), true);
  2524. VERIFY_EQUAL_NONCONT(modDoc->ModifiedSinceLastAutosave(), false);
  2525. sndFile.Destroy();
  2526. modDoc->OnCloseDocument();
  2527. #endif
  2528. }
  2529. static void RunITCompressionTest(const std::vector<int8> &sampleData, FlagSet<ChannelFlags> smpFormat, bool it215)
  2530. {
  2531. ModSample smp;
  2532. smp.uFlags = smpFormat;
  2533. smp.pData.pSample = const_cast<int8 *>(sampleData.data());
  2534. smp.nLength = mpt::saturate_cast<SmpLength>(sampleData.size() / smp.GetBytesPerSample());
  2535. std::string data;
  2536. {
  2537. std::ostringstream f;
  2538. ITCompression compression(smp, it215, &f);
  2539. data = f.str();
  2540. }
  2541. {
  2542. FileReader file(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(data)));
  2543. std::vector<int8> sampleDataNew(sampleData.size(), 0);
  2544. smp.pData.pSample = sampleDataNew.data();
  2545. ITDecompression decompression(file, smp, it215);
  2546. VERIFY_EQUAL_NONCONT(memcmp(sampleData.data(), sampleDataNew.data(), sampleData.size()), 0);
  2547. }
  2548. }
  2549. static MPT_NOINLINE void TestITCompression()
  2550. {
  2551. // Test loading / saving of IT-compressed samples
  2552. const int sampleDataSize = 65536;
  2553. std::vector<int8> sampleData(sampleDataSize, 0);
  2554. std::srand(0);
  2555. for(int i = 0; i < sampleDataSize; i++)
  2556. {
  2557. sampleData[i] = mpt::random<int8>(*s_PRNG);
  2558. }
  2559. // Run each compression test with IT215 compression and without.
  2560. for(int i = 0; i < 2; i++)
  2561. {
  2562. RunITCompressionTest(sampleData, ChannelFlags(0), i == 0);
  2563. RunITCompressionTest(sampleData, CHN_16BIT, i == 0);
  2564. RunITCompressionTest(sampleData, CHN_STEREO, i == 0);
  2565. RunITCompressionTest(sampleData, CHN_16BIT | CHN_STEREO, i == 0);
  2566. }
  2567. }
  2568. #if 0
  2569. static bool RatioEqual(CTuningBase::RATIOTYPE a, CTuningBase::RATIOTYPE b)
  2570. {
  2571. if(a == CTuningBase::RATIOTYPE(0) && b == CTuningBase::RATIOTYPE(0))
  2572. {
  2573. return true;
  2574. }
  2575. if(a == CTuningBase::RATIOTYPE(0) || b == CTuningBase::RATIOTYPE(0))
  2576. {
  2577. return false;
  2578. }
  2579. return (std::fabs(CTuningBase::RATIOTYPE(1) - (a/b)) < CTuningBase::RATIOTYPE(0.0001));
  2580. }
  2581. static void CheckEqualTuningCollections(const CTuningCollection &a, const CTuningCollection &b)
  2582. {
  2583. VERIFY_EQUAL(a.GetName(), b.GetName());
  2584. VERIFY_EQUAL(a.GetNumTunings(), b.GetNumTunings());
  2585. for(std::size_t tuning = 0; tuning < std::min(a.GetNumTunings(), b.GetNumTunings()); ++tuning)
  2586. {
  2587. VERIFY_EQUAL(a.GetTuning(tuning).GetName(), b.GetTuning(tuning).GetName());
  2588. VERIFY_EQUAL(a.GetTuning(tuning).GetType(), b.GetTuning(tuning).GetType());
  2589. VERIFY_EQUAL(a.GetTuning(tuning).GetGroupSize(), b.GetTuning(tuning).GetGroupSize());
  2590. VERIFY_EQUAL(a.GetTuning(tuning).GetFineStepCount(), b.GetTuning(tuning).GetFineStepCount());
  2591. VERIFY_EQUAL(RatioEqual(a.GetTuning(tuning).GetGroupRatio(), b.GetTuning(tuning).GetGroupRatio()), true);
  2592. VERIFY_EQUAL(a.GetTuning(tuning).GetValidityRange(), b.GetTuning(tuning).GetValidityRange());
  2593. for(ModCommand::NOTE note = NOTE_MIN; note <= NOTE_MAX; ++note)
  2594. {
  2595. VERIFY_EQUAL(a.GetTuning(tuning).GetNoteName(note - NOTE_MIDDLEC), b.GetTuning(tuning).GetNoteName(note - NOTE_MIDDLEC));
  2596. VERIFY_EQUAL(RatioEqual(a.GetTuning(tuning).GetRatio(note - NOTE_MIDDLEC), b.GetTuning(tuning).GetRatio(note - NOTE_MIDDLEC)), true);
  2597. }
  2598. }
  2599. }
  2600. #endif
  2601. static double Rand01()
  2602. {
  2603. return mpt::random(*s_PRNG, 0.0, 1.0);
  2604. }
  2605. template <class T>
  2606. T Rand(const T min, const T max)
  2607. {
  2608. return mpt::saturate_round<T>(min + Rand01() * (max - min));
  2609. }
  2610. static void GenerateCommands(CPattern& pat, const double dProbPcs, const double dProbPc)
  2611. {
  2612. const double dPcxProb = dProbPcs + dProbPc;
  2613. for(auto &m : pat)
  2614. {
  2615. const double rand = Rand01();
  2616. if(rand < dPcxProb)
  2617. {
  2618. if(rand < dProbPcs)
  2619. m.note = NOTE_PCS;
  2620. else
  2621. m.note = NOTE_PC;
  2622. m.instr = Rand<ModCommand::INSTR>(0, MAX_MIXPLUGINS);
  2623. m.SetValueVolCol(Rand<uint16>(0, ModCommand::maxColumnValue));
  2624. m.SetValueEffectCol(Rand<uint16>(0, ModCommand::maxColumnValue));
  2625. }
  2626. else
  2627. m.Clear();
  2628. }
  2629. }
  2630. // Test PC note serialization
  2631. static MPT_NOINLINE void TestPCnoteSerialization()
  2632. {
  2633. FileReader file;
  2634. std::unique_ptr<CSoundFile> pSndFile = std::make_unique<CSoundFile>();
  2635. CSoundFile &sndFile = *pSndFile.get();
  2636. sndFile.m_nType = MOD_TYPE_MPT;
  2637. sndFile.Patterns.DestroyPatterns();
  2638. sndFile.m_nChannels = ModSpecs::mptm.channelsMax;
  2639. sndFile.Patterns.Insert(0, ModSpecs::mptm.patternRowsMin);
  2640. sndFile.Patterns.Insert(1, 64);
  2641. GenerateCommands(sndFile.Patterns[1], 0.3, 0.3);
  2642. sndFile.Patterns.Insert(2, ModSpecs::mptm.patternRowsMax);
  2643. GenerateCommands(sndFile.Patterns[2], 0.5, 0.5);
  2644. // Copy pattern data for comparison.
  2645. CPatternContainer patterns{ sndFile.Patterns };
  2646. std::stringstream mem;
  2647. WriteModPatterns(mem, sndFile.Patterns);
  2648. VERIFY_EQUAL_NONCONT( mem.good(), true );
  2649. // Clear patterns.
  2650. sndFile.Patterns[0].ClearCommands();
  2651. sndFile.Patterns[1].ClearCommands();
  2652. sndFile.Patterns[2].ClearCommands();
  2653. // Read data back.
  2654. ReadModPatterns(mem, sndFile.Patterns);
  2655. // Compare.
  2656. VERIFY_EQUAL_NONCONT( sndFile.Patterns[0].GetNumRows(), ModSpecs::mptm.patternRowsMin);
  2657. VERIFY_EQUAL_NONCONT( sndFile.Patterns[1].GetNumRows(), 64);
  2658. VERIFY_EQUAL_NONCONT( sndFile.Patterns[2].GetNumRows(), ModSpecs::mptm.patternRowsMax);
  2659. for(int i = 0; i < 3; i++)
  2660. {
  2661. VERIFY_EQUAL(sndFile.Patterns[i], patterns[i]);
  2662. }
  2663. }
  2664. static inline std::size_t strnlen(const char *str, std::size_t n)
  2665. {
  2666. #if MPT_COMPILER_MSVC
  2667. return ::strnlen(str, n);
  2668. #else
  2669. if(n >= std::numeric_limits<std::size_t>::max())
  2670. {
  2671. return std::strlen(str);
  2672. }
  2673. for(std::size_t i = 0; i < n; ++i)
  2674. {
  2675. if(str[i] == '\0')
  2676. {
  2677. return i;
  2678. }
  2679. }
  2680. return n;
  2681. #endif
  2682. }
  2683. // Test String I/O functionality
  2684. static MPT_NOINLINE void TestStringIO()
  2685. {
  2686. char src0[4] = { '\0', 'X', ' ', 'X' }; // Weird empty buffer
  2687. char src1[4] = { 'X', ' ', '\0', 'X' }; // Weird buffer (hello Impulse Tracker)
  2688. char src2[4] = { 'X', 'Y', 'Z', ' ' }; // Full buffer, last character space
  2689. char src3[4] = { 'X', 'Y', 'Z', '!' }; // Full buffer, last character non-space
  2690. char src4[4] = { 'x', 'y', '\t', '\n' }; // Full buffer containing non-space whitespace
  2691. char dst1[6]; // Destination buffer, larger than source buffer
  2692. char dst2[3]; // Destination buffer, smaller than source buffer
  2693. #define ReadTest(mode, dst, src, expectedResult) \
  2694. std::memset(dst, 0x7f, sizeof(dst)); \
  2695. mpt::String::WriteAutoBuf(dst) = mpt::String::ReadBuf(mpt::String:: mode , src); \
  2696. VERIFY_EQUAL_NONCONT(strncmp(dst, expectedResult, std::size(dst)), 0); /* Ensure that the strings are identical */ \
  2697. for(size_t i = strlen(dst); i < std::size(dst); i++) \
  2698. VERIFY_EQUAL_NONCONT(dst[i], '\0'); /* Ensure that rest of the buffer is completely nulled */ \
  2699. /**/
  2700. #define WriteTest(mode, dst, src, expectedResult) \
  2701. std::memset(dst, 0x7f, sizeof(dst)); \
  2702. mpt::String::WriteBuf(mpt::String:: mode , dst) = mpt::String::ReadAutoBuf(src); \
  2703. VERIFY_EQUAL_NONCONT(strncmp(dst, expectedResult, std::size(dst)), 0); /* Ensure that the strings are identical */ \
  2704. for(size_t i = Test::strnlen(dst, std::size(dst)); i < std::size(dst); i++) \
  2705. VERIFY_EQUAL_NONCONT(dst[i], '\0'); /* Ensure that rest of the buffer is completely nulled */ \
  2706. /**/
  2707. // Check reading of null-terminated string into larger buffer
  2708. ReadTest(nullTerminated, dst1, src0, "");
  2709. ReadTest(nullTerminated, dst1, src1, "X ");
  2710. ReadTest(nullTerminated, dst1, src2, "XYZ");
  2711. ReadTest(nullTerminated, dst1, src3, "XYZ");
  2712. ReadTest(nullTerminated, dst1, src4, "xy\t");
  2713. // Check reading of string that should be null-terminated, but is maybe too long to still hold the null character.
  2714. ReadTest(maybeNullTerminated, dst1, src0, "");
  2715. ReadTest(maybeNullTerminated, dst1, src1, "X ");
  2716. ReadTest(maybeNullTerminated, dst1, src2, "XYZ ");
  2717. ReadTest(maybeNullTerminated, dst1, src3, "XYZ!");
  2718. ReadTest(maybeNullTerminated, dst1, src4, "xy\t\n");
  2719. // Check reading of space-padded strings with ignored last character
  2720. ReadTest(spacePaddedNull, dst1, src0, " X");
  2721. ReadTest(spacePaddedNull, dst1, src1, "X");
  2722. ReadTest(spacePaddedNull, dst1, src2, "XYZ");
  2723. ReadTest(spacePaddedNull, dst1, src3, "XYZ");
  2724. ReadTest(spacePaddedNull, dst1, src4, "xy\t");
  2725. // Check reading of space-padded strings
  2726. ReadTest(spacePadded, dst1, src0, " X X");
  2727. ReadTest(spacePadded, dst1, src1, "X X");
  2728. ReadTest(spacePadded, dst1, src2, "XYZ");
  2729. ReadTest(spacePadded, dst1, src3, "XYZ!");
  2730. ReadTest(spacePadded, dst1, src4, "xy\t\n");
  2731. ///////////////////////////////
  2732. // Check reading of null-terminated string into smaller buffer
  2733. ReadTest(nullTerminated, dst2, src0, "");
  2734. ReadTest(nullTerminated, dst2, src1, "X ");
  2735. ReadTest(nullTerminated, dst2, src2, "XY");
  2736. ReadTest(nullTerminated, dst2, src3, "XY");
  2737. ReadTest(nullTerminated, dst2, src4, "xy");
  2738. // Check reading of string that should be null-terminated, but is maybe too long to still hold the null character.
  2739. ReadTest(maybeNullTerminated, dst2, src0, "");
  2740. ReadTest(maybeNullTerminated, dst2, src1, "X ");
  2741. ReadTest(maybeNullTerminated, dst2, src2, "XY");
  2742. ReadTest(maybeNullTerminated, dst2, src3, "XY");
  2743. ReadTest(maybeNullTerminated, dst2, src4, "xy");
  2744. // Check reading of space-padded strings with ignored last character
  2745. ReadTest(spacePaddedNull, dst2, src0, " X");
  2746. ReadTest(spacePaddedNull, dst2, src1, "X");
  2747. ReadTest(spacePaddedNull, dst2, src2, "XY");
  2748. ReadTest(spacePaddedNull, dst2, src3, "XY");
  2749. ReadTest(spacePaddedNull, dst2, src4, "xy");
  2750. // Check reading of space-padded strings
  2751. ReadTest(spacePadded, dst2, src0, " X");
  2752. ReadTest(spacePadded, dst2, src1, "X ");
  2753. ReadTest(spacePadded, dst2, src2, "XY");
  2754. ReadTest(spacePadded, dst2, src3, "XY");
  2755. ReadTest(spacePadded, dst2, src4, "xy");
  2756. ///////////////////////////////
  2757. // Check writing of null-terminated string into larger buffer
  2758. WriteTest(nullTerminated, dst1, src0, "");
  2759. WriteTest(nullTerminated, dst1, src1, "X ");
  2760. WriteTest(nullTerminated, dst1, src2, "XYZ ");
  2761. WriteTest(nullTerminated, dst1, src3, "XYZ!");
  2762. // Check writing of string that should be null-terminated, but is maybe too long to still hold the null character.
  2763. WriteTest(maybeNullTerminated, dst1, src0, "");
  2764. WriteTest(maybeNullTerminated, dst1, src1, "X ");
  2765. WriteTest(maybeNullTerminated, dst1, src2, "XYZ ");
  2766. WriteTest(maybeNullTerminated, dst1, src3, "XYZ!");
  2767. // Check writing of space-padded strings with last character set to null
  2768. WriteTest(spacePaddedNull, dst1, src0, " ");
  2769. WriteTest(spacePaddedNull, dst1, src1, "X ");
  2770. WriteTest(spacePaddedNull, dst1, src2, "XYZ ");
  2771. WriteTest(spacePaddedNull, dst1, src3, "XYZ! ");
  2772. // Check writing of space-padded strings
  2773. WriteTest(spacePadded, dst1, src0, " ");
  2774. WriteTest(spacePadded, dst1, src1, "X ");
  2775. WriteTest(spacePadded, dst1, src2, "XYZ ");
  2776. WriteTest(spacePadded, dst1, src3, "XYZ! ");
  2777. ///////////////////////////////
  2778. // Check writing of null-terminated string into smaller buffer
  2779. WriteTest(nullTerminated, dst2, src0, "");
  2780. WriteTest(nullTerminated, dst2, src1, "X ");
  2781. WriteTest(nullTerminated, dst2, src2, "XY");
  2782. WriteTest(nullTerminated, dst2, src3, "XY");
  2783. // Check writing of string that should be null-terminated, but is maybe too long to still hold the null character.
  2784. WriteTest(maybeNullTerminated, dst2, src0, "");
  2785. WriteTest(maybeNullTerminated, dst2, src1, "X ");
  2786. WriteTest(maybeNullTerminated, dst2, src2, "XYZ");
  2787. WriteTest(maybeNullTerminated, dst2, src3, "XYZ");
  2788. // Check writing of space-padded strings with last character set to null
  2789. WriteTest(spacePaddedNull, dst2, src0, " ");
  2790. WriteTest(spacePaddedNull, dst2, src1, "X ");
  2791. WriteTest(spacePaddedNull, dst2, src2, "XY");
  2792. WriteTest(spacePaddedNull, dst2, src3, "XY");
  2793. // Check writing of space-padded strings
  2794. WriteTest(spacePadded, dst2, src0, " ");
  2795. WriteTest(spacePadded, dst2, src1, "X ");
  2796. WriteTest(spacePadded, dst2, src2, "XYZ");
  2797. WriteTest(spacePadded, dst2, src3, "XYZ");
  2798. #undef ReadTest
  2799. #undef WriteTest
  2800. {
  2801. std::string dststring;
  2802. std::string src0string = std::string(src0, std::size(src0));
  2803. std::string src1string = std::string(src1, std::size(src1));
  2804. std::string src2string = std::string(src2, std::size(src2));
  2805. std::string src3string = std::string(src3, std::size(src3));
  2806. #define ReadTest(mode, dst, src, expectedResult) \
  2807. dst = mpt::String::ReadBuf(mpt::String:: mode , src); \
  2808. VERIFY_EQUAL_NONCONT(dst, expectedResult); /* Ensure that the strings are identical */ \
  2809. /**/
  2810. #define WriteTest(mode, dst, src, expectedResult) \
  2811. std::memset(dst, 0x7f, sizeof(dst)); \
  2812. mpt::String::WriteBuf(mpt::String:: mode , dst) = src; \
  2813. VERIFY_EQUAL_NONCONT(strncmp(dst, expectedResult, std::size(dst)), 0); /* Ensure that the strings are identical */ \
  2814. for(size_t i = Test::strnlen(dst, std::size(dst)); i < std::size(dst); i++) \
  2815. VERIFY_EQUAL_NONCONT(dst[i], '\0'); /* Ensure that rest of the buffer is completely nulled */ \
  2816. /**/
  2817. // Check reading of null-terminated string into std::string
  2818. ReadTest(nullTerminated, dststring, src0, "");
  2819. ReadTest(nullTerminated, dststring, src1, "X ");
  2820. ReadTest(nullTerminated, dststring, src2, "XYZ");
  2821. ReadTest(nullTerminated, dststring, src3, "XYZ");
  2822. ReadTest(nullTerminated, dststring, src4, "xy\t");
  2823. // Check reading of string that should be null-terminated, but is maybe too long to still hold the null character.
  2824. ReadTest(maybeNullTerminated, dststring, src0, "");
  2825. ReadTest(maybeNullTerminated, dststring, src1, "X ");
  2826. ReadTest(maybeNullTerminated, dststring, src2, "XYZ ");
  2827. ReadTest(maybeNullTerminated, dststring, src3, "XYZ!");
  2828. ReadTest(maybeNullTerminated, dststring, src4, "xy\t\n");
  2829. // Check reading of space-padded strings with ignored last character
  2830. ReadTest(spacePaddedNull, dststring, src0, " X");
  2831. ReadTest(spacePaddedNull, dststring, src1, "X");
  2832. ReadTest(spacePaddedNull, dststring, src2, "XYZ");
  2833. ReadTest(spacePaddedNull, dststring, src3, "XYZ");
  2834. ReadTest(spacePaddedNull, dststring, src4, "xy\t");
  2835. // Check reading of space-padded strings
  2836. ReadTest(spacePadded, dststring, src0, " X X");
  2837. ReadTest(spacePadded, dststring, src1, "X X");
  2838. ReadTest(spacePadded, dststring, src2, "XYZ");
  2839. ReadTest(spacePadded, dststring, src3, "XYZ!");
  2840. ReadTest(spacePadded, dststring, src4, "xy\t\n");
  2841. ///////////////////////////////
  2842. // Check writing of null-terminated string into larger buffer
  2843. WriteTest(nullTerminated, dst1, src0string, "");
  2844. WriteTest(nullTerminated, dst1, src1string, "X ");
  2845. WriteTest(nullTerminated, dst1, src2string, "XYZ ");
  2846. WriteTest(nullTerminated, dst1, src3string, "XYZ!");
  2847. // Check writing of string that should be null-terminated, but is maybe too long to still hold the null character.
  2848. WriteTest(maybeNullTerminated, dst1, src0string, "");
  2849. WriteTest(maybeNullTerminated, dst1, src1string, "X ");
  2850. WriteTest(maybeNullTerminated, dst1, src2string, "XYZ ");
  2851. WriteTest(maybeNullTerminated, dst1, src3string, "XYZ!");
  2852. // Check writing of space-padded strings with last character set to null
  2853. WriteTest(spacePaddedNull, dst1, src0string, " ");
  2854. WriteTest(spacePaddedNull, dst1, src1string, "X ");
  2855. WriteTest(spacePaddedNull, dst1, src2string, "XYZ ");
  2856. WriteTest(spacePaddedNull, dst1, src3string, "XYZ! ");
  2857. // Check writing of space-padded strings
  2858. WriteTest(spacePadded, dst1, src0string, " ");
  2859. WriteTest(spacePadded, dst1, src1string, "X ");
  2860. WriteTest(spacePadded, dst1, src2string, "XYZ ");
  2861. WriteTest(spacePadded, dst1, src3string, "XYZ! ");
  2862. ///////////////////////////////
  2863. // Check writing of null-terminated string into smaller buffer
  2864. WriteTest(nullTerminated, dst2, src0string, "");
  2865. WriteTest(nullTerminated, dst2, src1string, "X ");
  2866. WriteTest(nullTerminated, dst2, src2string, "XY");
  2867. WriteTest(nullTerminated, dst2, src3string, "XY");
  2868. // Check writing of string that should be null-terminated, but is maybe too long to still hold the null character.
  2869. WriteTest(maybeNullTerminated, dst2, src0string, "");
  2870. WriteTest(maybeNullTerminated, dst2, src1string, "X ");
  2871. WriteTest(maybeNullTerminated, dst2, src2string, "XYZ");
  2872. WriteTest(maybeNullTerminated, dst2, src3string, "XYZ");
  2873. // Check writing of space-padded strings with last character set to null
  2874. WriteTest(spacePaddedNull, dst2, src0string, " ");
  2875. WriteTest(spacePaddedNull, dst2, src1string, "X ");
  2876. WriteTest(spacePaddedNull, dst2, src2string, "XY");
  2877. WriteTest(spacePaddedNull, dst2, src3string, "XY");
  2878. // Check writing of space-padded strings
  2879. WriteTest(spacePadded, dst2, src0string, " ");
  2880. WriteTest(spacePadded, dst2, src1string, "X ");
  2881. WriteTest(spacePadded, dst2, src2string, "XYZ");
  2882. WriteTest(spacePadded, dst2, src3string, "XYZ");
  2883. ///////////////////////////////
  2884. #undef ReadTest
  2885. #undef WriteTest
  2886. }
  2887. {
  2888. char s0[4] = {'\0', 'X', ' ', 'X' };
  2889. char s2[4] = { 'X', ' ','\0', 'X' };
  2890. char s4[4] = { 'X', 'Y', 'Z', ' ' };
  2891. char d2[2] = {'\0','\0'};
  2892. char d3[3] = {'\0','\0','\0'};
  2893. char d4[4] = {'\0','\0','\0','\0'};
  2894. char d5[5] = {'\0','\0','\0','\0','\0'};
  2895. #define CopyTest(dst, src, expectedResult) \
  2896. std::memset(dst, 0x7f, sizeof(dst)); \
  2897. mpt::String::WriteAutoBuf(dst) = mpt::String::ReadAutoBuf(src); \
  2898. VERIFY_EQUAL_NONCONT(strncmp(dst, expectedResult, std::size(dst)), 0); /* Ensure that the strings are identical */ \
  2899. for(size_t i = strlen(dst); i < std::size(dst); i++) \
  2900. VERIFY_EQUAL_NONCONT(dst[i], '\0'); /* Ensure that rest of the buffer is completely nulled */ \
  2901. /**/
  2902. CopyTest(d2, s0, "");
  2903. CopyTest(d2, s2, "X");
  2904. CopyTest(d2, s4, "X");
  2905. CopyTest(d3, s0, "");
  2906. CopyTest(d3, s2, "X ");
  2907. CopyTest(d3, s4, "XY");
  2908. CopyTest(d4, s0, "");
  2909. CopyTest(d4, s2, "X ");
  2910. CopyTest(d4, s4, "XYZ");
  2911. CopyTest(d5, s0, "");
  2912. CopyTest(d5, s2, "X ");
  2913. CopyTest(d5, s4, "XYZ ");
  2914. #undef CopyTest
  2915. #define CopyTestN(dst, src, len, expectedResult) \
  2916. std::memset(dst, 0x7f, sizeof(dst)); \
  2917. mpt::String::WriteAutoBuf(dst) = mpt::String::ReadAutoBuf(src, std::min(std::size(src), static_cast<std::size_t>(len))); \
  2918. VERIFY_EQUAL_NONCONT(strncmp(dst, expectedResult, std::size(dst)), 0); /* Ensure that the strings are identical */ \
  2919. for(size_t i = strlen(dst); i < std::size(dst); i++) \
  2920. VERIFY_EQUAL_NONCONT(dst[i], '\0'); /* Ensure that rest of the buffer is completely nulled */ \
  2921. /**/
  2922. CopyTestN(d2, s0, 1, "");
  2923. CopyTestN(d2, s2, 1, "X");
  2924. CopyTestN(d2, s4, 1, "X");
  2925. CopyTestN(d3, s0, 1, "");
  2926. CopyTestN(d3, s2, 1, "X");
  2927. CopyTestN(d3, s4, 1, "X");
  2928. CopyTestN(d4, s0, 1, "");
  2929. CopyTestN(d4, s2, 1, "X");
  2930. CopyTestN(d4, s4, 1, "X");
  2931. CopyTestN(d5, s0, 1, "");
  2932. CopyTestN(d5, s2, 1, "X");
  2933. CopyTestN(d5, s4, 1, "X");
  2934. CopyTestN(d2, s0, 2, "");
  2935. CopyTestN(d2, s2, 2, "X");
  2936. CopyTestN(d2, s4, 2, "X");
  2937. CopyTestN(d3, s0, 2, "");
  2938. CopyTestN(d3, s2, 2, "X ");
  2939. CopyTestN(d3, s4, 2, "XY");
  2940. CopyTestN(d4, s0, 2, "");
  2941. CopyTestN(d4, s2, 2, "X ");
  2942. CopyTestN(d4, s4, 2, "XY");
  2943. CopyTestN(d5, s0, 2, "");
  2944. CopyTestN(d5, s2, 2, "X ");
  2945. CopyTestN(d5, s4, 2, "XY");
  2946. CopyTestN(d2, s0, 3, "");
  2947. CopyTestN(d2, s2, 3, "X");
  2948. CopyTestN(d2, s4, 3, "X");
  2949. CopyTestN(d3, s0, 3, "");
  2950. CopyTestN(d3, s2, 3, "X ");
  2951. CopyTestN(d3, s4, 3, "XY");
  2952. CopyTestN(d4, s0, 3, "");
  2953. CopyTestN(d4, s2, 3, "X ");
  2954. CopyTestN(d4, s4, 3, "XYZ");
  2955. CopyTestN(d5, s0, 3, "");
  2956. CopyTestN(d5, s2, 3, "X ");
  2957. CopyTestN(d5, s4, 3, "XYZ");
  2958. CopyTestN(d2, s0, 4, "");
  2959. CopyTestN(d2, s2, 4, "X");
  2960. CopyTestN(d2, s4, 4, "X");
  2961. CopyTestN(d3, s0, 4, "");
  2962. CopyTestN(d3, s2, 4, "X ");
  2963. CopyTestN(d3, s4, 4, "XY");
  2964. CopyTestN(d4, s0, 4, "");
  2965. CopyTestN(d4, s2, 4, "X ");
  2966. CopyTestN(d4, s4, 4, "XYZ");
  2967. CopyTestN(d5, s0, 4, "");
  2968. CopyTestN(d5, s2, 4, "X ");
  2969. CopyTestN(d5, s4, 4, "XYZ ");
  2970. CopyTestN(d2, s0, 5, "");
  2971. CopyTestN(d2, s2, 5, "X");
  2972. CopyTestN(d2, s4, 5, "X");
  2973. CopyTestN(d3, s0, 5, "");
  2974. CopyTestN(d3, s2, 5, "X ");
  2975. CopyTestN(d3, s4, 5, "XY");
  2976. CopyTestN(d4, s0, 5, "");
  2977. CopyTestN(d4, s2, 5, "X ");
  2978. CopyTestN(d4, s4, 5, "XYZ");
  2979. CopyTestN(d5, s0, 5, "");
  2980. CopyTestN(d5, s2, 5, "X ");
  2981. CopyTestN(d5, s4, 5, "XYZ ");
  2982. #undef CopyTest
  2983. }
  2984. }
  2985. static MPT_NOINLINE void TestSampleConversion()
  2986. {
  2987. std::vector<uint8> sourceBufContainer(65536 * 4);
  2988. std::vector<uint8> targetBufContainer(65536 * 6);
  2989. uint8 *sourceBuf = &(sourceBufContainer[0]);
  2990. void *targetBuf = &(targetBufContainer[0]);
  2991. // Signed 8-Bit Integer PCM
  2992. // Unsigned 8-Bit Integer PCM
  2993. // Delta 8-Bit Integer PCM
  2994. {
  2995. uint8 *source8 = sourceBuf;
  2996. for(size_t i = 0; i < 256; i++)
  2997. {
  2998. source8[i] = static_cast<uint8>(i);
  2999. }
  3000. int8 *signed8 = static_cast<int8 *>(targetBuf);
  3001. uint8 *unsigned8 = static_cast<uint8 *>(targetBuf) + 256;
  3002. int8 *delta8 = static_cast<int8 *>(targetBuf) + 512;
  3003. int8 delta = 0;
  3004. CopySample<SC::DecodeInt8>(signed8, 256, 1, mpt::byte_cast<const std::byte *>(source8), 256, 1);
  3005. CopySample<SC::DecodeUint8>(reinterpret_cast<int8 *>(unsigned8), 256, 1, mpt::byte_cast<const std::byte *>(source8), 256, 1);
  3006. CopySample<SC::DecodeInt8Delta>(delta8, 256, 1, mpt::byte_cast<const std::byte *>(source8), 256, 1);
  3007. for(size_t i = 0; i < 256; i++)
  3008. {
  3009. delta += static_cast<int8>(i);
  3010. VERIFY_EQUAL_QUIET_NONCONT(signed8[i], static_cast<int8>(i));
  3011. VERIFY_EQUAL_QUIET_NONCONT(unsigned8[i], static_cast<uint8>(i + 0x80u));
  3012. VERIFY_EQUAL_QUIET_NONCONT(delta8[i], static_cast<int8>(delta));
  3013. }
  3014. }
  3015. // Signed 16-Bit Integer PCM
  3016. // Unsigned 16-Bit Integer PCM
  3017. // Delta 16-Bit Integer PCM
  3018. {
  3019. // Little Endian
  3020. uint8 *source16 = sourceBuf;
  3021. for(size_t i = 0; i < 65536; i++)
  3022. {
  3023. source16[i * 2 + 0] = static_cast<uint8>(i & 0xFF);
  3024. source16[i * 2 + 1] = static_cast<uint8>(i >> 8);
  3025. }
  3026. int16 *signed16 = static_cast<int16 *>(targetBuf);
  3027. uint16 *unsigned16 = static_cast<uint16 *>(targetBuf) + 65536;
  3028. int16 *delta16 = static_cast<int16 *>(targetBuf) + 65536 * 2;
  3029. int16 delta = 0;
  3030. CopySample<SC::DecodeInt16<0, littleEndian16> >(signed16, 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3031. CopySample<SC::DecodeInt16<0x8000u, littleEndian16> >(reinterpret_cast<int16*>(unsigned16), 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3032. CopySample<SC::DecodeInt16Delta<littleEndian16> >(delta16, 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3033. for(size_t i = 0; i < 65536; i++)
  3034. {
  3035. delta += static_cast<int16>(i);
  3036. VERIFY_EQUAL_QUIET_NONCONT(signed16[i], static_cast<int16>(i));
  3037. VERIFY_EQUAL_QUIET_NONCONT(unsigned16[i], static_cast<uint16>(i + 0x8000u));
  3038. VERIFY_EQUAL_QUIET_NONCONT(delta16[i], static_cast<int16>(delta));
  3039. }
  3040. // Big Endian
  3041. for(size_t i = 0; i < 65536; i++)
  3042. {
  3043. source16[i * 2 + 0] = static_cast<uint8>(i >> 8);
  3044. source16[i * 2 + 1] = static_cast<uint8>(i & 0xFF);
  3045. }
  3046. CopySample<SC::DecodeInt16<0, bigEndian16> >(signed16, 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3047. CopySample<SC::DecodeInt16<0x8000u, bigEndian16> >(reinterpret_cast<int16*>(unsigned16), 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3048. CopySample<SC::DecodeInt16Delta<bigEndian16> >(delta16, 65536, 1, mpt::byte_cast<const std::byte *>(source16), 65536 * 2, 1);
  3049. delta = 0;
  3050. for(size_t i = 0; i < 65536; i++)
  3051. {
  3052. delta += static_cast<int16>(i);
  3053. VERIFY_EQUAL_QUIET_NONCONT(signed16[i], static_cast<int16>(i));
  3054. VERIFY_EQUAL_QUIET_NONCONT(unsigned16[i], static_cast<uint16>(i + 0x8000u));
  3055. VERIFY_EQUAL_QUIET_NONCONT(delta16[i], static_cast<int16>(delta));
  3056. }
  3057. }
  3058. // Signed 24-Bit Integer PCM
  3059. {
  3060. uint8 *source24 = sourceBuf;
  3061. for(size_t i = 0; i < 65536; i++)
  3062. {
  3063. source24[i * 3 + 0] = 0;
  3064. source24[i * 3 + 1] = static_cast<uint8>(i & 0xFF);
  3065. source24[i * 3 + 2] = static_cast<uint8>(i >> 8);
  3066. }
  3067. int16 *truncated16 = static_cast<int16 *>(targetBuf);
  3068. ModSample sample;
  3069. sample.Initialize();
  3070. sample.nLength = 65536;
  3071. sample.uFlags.set(CHN_16BIT);
  3072. sample.pData.pSample = (static_cast<int16 *>(targetBuf) + 65536);
  3073. CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, int32>, SC::DecodeInt24<0, littleEndian24> > >(sample, mpt::byte_cast<const std::byte *>(source24), 3*65536);
  3074. CopySample<SC::ConversionChain<SC::ConvertShift<int16, int32, 16>, SC::DecodeInt24<0, littleEndian24> > >(truncated16, 65536, 1, mpt::byte_cast<const std::byte *>(source24), 65536 * 3, 1);
  3075. for(size_t i = 0; i < 65536; i++)
  3076. {
  3077. VERIFY_EQUAL_QUIET_NONCONT(sample.sample16()[i], static_cast<int16>(i));
  3078. VERIFY_EQUAL_QUIET_NONCONT(truncated16[i], static_cast<int16>(i));
  3079. }
  3080. }
  3081. // Float 32-Bit
  3082. {
  3083. uint8 *source32 = sourceBuf;
  3084. for(size_t i = 0; i < 65536; i++)
  3085. {
  3086. IEEE754binary32BE floatbits = IEEE754binary32BE((static_cast<float>(i) / 65536.0f) - 0.5f);
  3087. source32[i * 4 + 0] = mpt::byte_cast<uint8>(floatbits.GetByte(0));
  3088. source32[i * 4 + 1] = mpt::byte_cast<uint8>(floatbits.GetByte(1));
  3089. source32[i * 4 + 2] = mpt::byte_cast<uint8>(floatbits.GetByte(2));
  3090. source32[i * 4 + 3] = mpt::byte_cast<uint8>(floatbits.GetByte(3));
  3091. }
  3092. int16 *truncated16 = static_cast<int16 *>(targetBuf);
  3093. ModSample sample;
  3094. sample.Initialize();
  3095. sample.nLength = 65536;
  3096. sample.uFlags.set(CHN_16BIT);
  3097. sample.pData.pSample = static_cast<int16 *>(targetBuf) + 65536;
  3098. CopyAndNormalizeSample<SC::NormalizationChain<SC::Convert<int16, float32>, SC::DecodeFloat32<bigEndian32> > >(sample, mpt::byte_cast<const std::byte *>(source32), 4*65536);
  3099. CopySample<SC::ConversionChain<SC::Convert<int16, float32>, SC::DecodeFloat32<bigEndian32> > >(truncated16, 65536, 1, mpt::byte_cast<const std::byte *>(source32), 65536 * 4, 1);
  3100. for(size_t i = 0; i < 65536; i++)
  3101. {
  3102. VERIFY_EQUAL_QUIET_NONCONT(sample.sample16()[i], static_cast<int16>(i - 0x8000u));
  3103. VERIFY_EQUAL_QUIET_NONCONT(std::abs(truncated16[i] - static_cast<int16>((i - 0x8000u) / 2)) <= 1, true);
  3104. }
  3105. }
  3106. // ALaw
  3107. {
  3108. for(unsigned int i = 0; i < 256; ++i)
  3109. {
  3110. std::byte in = mpt::byte_cast<std::byte>(static_cast<uint8>(i));
  3111. std::byte out = SC::EncodeALaw{}(SC::DecodeInt16ALaw{}(&in));
  3112. VERIFY_EQUAL_NONCONT(in, out);
  3113. }
  3114. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(-32768), SC::EncodeALaw{}(-32256));
  3115. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(-32767), SC::EncodeALaw{}(-32256));
  3116. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(-1), SC::EncodeALaw{}(-8));
  3117. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(0), SC::EncodeALaw{}(8));
  3118. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(1), SC::EncodeALaw{}(8));
  3119. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(32766), SC::EncodeALaw{}(32256));
  3120. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(32767), SC::EncodeALaw{}(32256));
  3121. #if 0
  3122. // compare with reference impl
  3123. for (int i = -32768; i <= 32767; ++i)
  3124. {
  3125. VERIFY_EQUAL_NONCONT(SC::EncodeALaw{}(i), mpt::byte_cast<std::byte>(alaw_encode(i)));
  3126. }
  3127. #endif
  3128. }
  3129. // uLaw
  3130. {
  3131. for(unsigned int i = 0; i < 256; ++i)
  3132. {
  3133. std::byte in = mpt::byte_cast<std::byte>(static_cast<uint8>(i));
  3134. std::byte out = SC::EncodeuLaw{}(SC::DecodeInt16uLaw{}(&in));
  3135. VERIFY_EQUAL_NONCONT(in, out);
  3136. }
  3137. #if 0
  3138. // compare with reference impl
  3139. /*
  3140. bool lastMatch = true;
  3141. */
  3142. for(int i = -32768; i <= 32767; ++i)
  3143. {
  3144. /*
  3145. uint8 mine = mpt::byte_cast<uint8>(SC::EncodeuLaw{}(i));
  3146. uint8 ref = ulaw_encode(i);
  3147. if(lastMatch)
  3148. {
  3149. if(mine == ref)
  3150. {
  3151. VERIFY_EQUAL_NONCONT(mine, ref);
  3152. lastMatch = true;
  3153. } else
  3154. {
  3155. VERIFY_EQUAL_NONCONT(std::abs(static_cast<int>(mine) - static_cast<int>(ref)) <= 1, true);
  3156. lastMatch = false;
  3157. }
  3158. } else
  3159. {
  3160. VERIFY_EQUAL_NONCONT(mine, ref);
  3161. lastMatch = true;
  3162. }
  3163. */
  3164. //MPT_LOG_GLOBAL(LogNotification, "test", MPT_UFORMAT("{} {} {}")(i, ulaw_encode(i), mpt::byte_cast<uint8>(SC::EncodeuLaw{}(i))));
  3165. VERIFY_EQUAL_NONCONT(SC::EncodeuLaw{}(i), mpt::byte_cast<std::byte>(ulaw_encode(i)));
  3166. }
  3167. #endif
  3168. }
  3169. // Range checks
  3170. {
  3171. int8 oneSample = 1;
  3172. char *signed8 = reinterpret_cast<char *>(targetBuf);
  3173. memset(signed8, 0, 4);
  3174. CopySample<SC::DecodeInt8>(reinterpret_cast<int8*>(targetBuf), 4, 1, reinterpret_cast<const std::byte*>(&oneSample), sizeof(oneSample), 1);
  3175. VERIFY_EQUAL_NONCONT(signed8[0], 1);
  3176. VERIFY_EQUAL_NONCONT(signed8[1], 0);
  3177. VERIFY_EQUAL_NONCONT(signed8[2], 0);
  3178. VERIFY_EQUAL_NONCONT(signed8[3], 0);
  3179. }
  3180. // Dither
  3181. {
  3182. std::vector<MixSampleInt> buffer(64);
  3183. DithersOpenMPT dithers(mpt::global_random_device(), 2 /* DitherModPlug */ , 2);
  3184. for(std::size_t i = 0; i < 64; ++i)
  3185. {
  3186. std::visit(
  3187. [&](auto &dither)
  3188. {
  3189. buffer[i] = dither.template process<16>(0, buffer[i]);
  3190. },
  3191. dithers.Variant()
  3192. );
  3193. }
  3194. std::vector<MixSampleInt> expected = {
  3195. 727,
  3196. -557,
  3197. -552,
  3198. -727,
  3199. 439,
  3200. 405,
  3201. 703,
  3202. -337,
  3203. 235,
  3204. -776,
  3205. -458,
  3206. 905,
  3207. -110,
  3208. 158,
  3209. 374,
  3210. -362,
  3211. 283,
  3212. 306,
  3213. 710,
  3214. 304,
  3215. -608,
  3216. 536,
  3217. -501,
  3218. -593,
  3219. -349,
  3220. 812,
  3221. 916,
  3222. 53,
  3223. -953,
  3224. 881,
  3225. -236,
  3226. -20,
  3227. -623,
  3228. -895,
  3229. -302,
  3230. -415,
  3231. 899,
  3232. -948,
  3233. -766,
  3234. -186,
  3235. -390,
  3236. -169,
  3237. 253,
  3238. -622,
  3239. -769,
  3240. -1001,
  3241. 1019,
  3242. 787,
  3243. -239,
  3244. 718,
  3245. -423,
  3246. 988,
  3247. -91,
  3248. 763,
  3249. -933,
  3250. -510,
  3251. 484,
  3252. 794,
  3253. -340,
  3254. 552,
  3255. 866,
  3256. -608,
  3257. 35,
  3258. 395};
  3259. for(std::size_t i = 0; i < 64; ++i)
  3260. {
  3261. VERIFY_EQUAL_QUIET_NONCONT(buffer[i], expected[i]);
  3262. }
  3263. }
  3264. }
  3265. } // namespace Test
  3266. OPENMPT_NAMESPACE_END
  3267. #else //Case: ENABLE_TESTS is not defined.
  3268. OPENMPT_NAMESPACE_BEGIN
  3269. namespace Test {
  3270. void DoTests()
  3271. {
  3272. return;
  3273. }
  3274. } // namespace Test
  3275. OPENMPT_NAMESPACE_END
  3276. #endif