TStringList |
Top Previous Next |
Unit tests > TStringList A frequently used Delphi class is TStringList. The translation of the defining code in System.Classes needs little manual post-processing. However there are some streaming operations namely in the base class TPersitent, which aren't implemented. But the example from
http://www.delphibasics.co.uk/RTL.asp?Name=tstringlist
compiles and works without manual post-processing. (Again, the original code has been slightly modified for the testing purpose.)
#include "dbsc_tstringlist.h" #include "d2c_convert.h"
using namespace std; using namespace System; using namespace System::Classes;
namespace dbsc_tstringlist { bool TStringListTest1() { bool result = false; TStringList* animals = nullptr; // Define our string list variable int i = 0; result = true; // Define a string list object, and point our variable at it animals = new TStringList();
// Now add some names to our list animals->Add(L"Cat"); animals->Add(L"Mouse"); animals->Add(L"Giraffe");
// Now display these animals // for i := 0 to animals.Count-1 do // ShowMessage(animals[i]); // animals[i] equates to animals.Strings[i] result = result && (animals->ReadPropertyStrings(0) == L"Cat"); result = result && (animals->ReadPropertyStrings(1) == L"Mouse"); result = result && (animals->ReadPropertyStrings(2) == L"Giraffe");
// Free up the list object delete animals; return result; }
bool TStringListTest2() { bool result = false; TStringList* Names = nullptr; // Define our string list variable String ageStr; int i = 0; int stop = 0; result = true; // Define a string list object, and point our variable at it Names = new TStringList();
// Now add some names to our list Names->WritePropertyCommaText(L"Neil=45, Brian=63, Jim=22");
// And now find Brian's age ageStr = Names->ReadPropertyValues(L"Brian");
// Display this value // ShowMessage('Brians age = '+ageStr); result = result && (ageStr == L"63");
// Now display all name and age pair values for(stop = Names->ReadPropertyCount() - 1, i = 0; i <= stop; i++) { //ShowMessage(names.Names[i]+' is '+names.ValueFromIndex[i]); if(i == 0) result = result && (String(ustr2pwchar(Names->ReadPropertyNames(i))) == L"Neil") && (String(ustr2pwchar(Names->ReadPropertyValueFromIndex(i))) == L"45"); if(i == 1) result = result && (String(ustr2pwchar(Names->ReadPropertyNames(i))) == L"Brian") && (String(ustr2pwchar(Names->ReadPropertyValueFromIndex(i))) == L"63"); if(i == 2) result = result && (String(ustr2pwchar(Names->ReadPropertyNames(i))) == L"Jim") && (String(ustr2pwchar(Names->ReadPropertyValueFromIndex(i))) == L"22"); }
// Free up the list object delete Names; return result; }
bool TStringListTest3() { bool result = false; TStringList* cars = nullptr; // Define our string list variable int i = 0; result = true; // Define a string list object, and point our variable at it cars = new TStringList();
// Now add some cars to our list - using the DelimitedText property // with overriden control variables cars->WritePropertyDelimiter(L' '); // Each list item will be blank separated cars->WritePropertyQuoteChar(L'|'); // And each item will be quoted with |'s cars->WritePropertyDelimitedText(L"|Honda Jazz| |Ford Mondeo| |Jaguar \"E-type\"|");
// Now display these cars // for i := 0 to cars.Count-1 do // ShowMessage(cars[i]); // cars[i] equates to cars.Strings[i] result = result && (cars->ReadPropertyStrings(0) == L"Honda Jazz"); result = result && (cars->ReadPropertyStrings(1) == L"Ford Mondeo"); result = result && (cars->ReadPropertyStrings(2) == L"Jaguar \"E-type\"");
// Free up the list object delete cars; return result; }
bool TStringListTest() { bool result = false; result = true; result = result && TStringListTest1(); result = result && TStringListTest2(); result = result && TStringListTest3(); return result; } } // namespace dbsc_tstringlist
|
This page belongs to the Delphi2Cpp Documentation |
Delphi2Cpp home Content |