//+------------------------------------------------------------------+
//|                                                TestChartText.mq5 |
//|                                       Copyright 2026, Ali Rajput |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Ali Rajput"
#property version   "1.10"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <ChartText\CanvasBlock.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input int InpRandomTexts = 300;  // random texts per property check
//+------------------------------------------------------------------+
//| Defines                                                          |
//+------------------------------------------------------------------+
#define BAD_LOSS       1    // characters lost or added, spaces, tabs and line breaks ignored
#define BAD_TOO_LONG   2    // a line is longer than the cap or wider than the limit
#define BAD_WORD_CUT   4    // a word that fits on a line was cut
#define BAD_SPLIT      8    // a line starts or ends inside a character (pair, mark, joiner, flag)
//+------------------------------------------------------------------+
//| Global variables                                                 |
//+------------------------------------------------------------------+
int    g_pass=0;     // checks that passed
int    g_fail=0;     // checks that failed
uint   g_seed=12345; // state of the random generator
string g_emoji;      // one character outside the BMP, two code units
string g_family;     // man, woman and girl joined by two zero width joiners, 8 code units
string g_flag;       // two regional indicators that form one flag, 4 code units
string g_thumb;      // a thumb with a skin tone modifier, 4 code units
string g_accent;     // the letter e and a combining acute accent, 2 code units
//+------------------------------------------------------------------+
//| CBumpyMeter                                                      |
//| A meter that breaks the assumption of the prefix search: every   |
//| fourth code unit makes the text 25 pixels narrower.              |
//+------------------------------------------------------------------+
class CBumpyMeter : public ITextMeter
  {
public:
//--- methods of the interface, written in the class body
   virtual int       Width(const string text) { int n=StringLen(text); return(n*10-((n%4==0 && n>0) ? 25 : 0)); }
   virtual int       LineHeight(void) { return(16); }
  };
//+------------------------------------------------------------------+
//| Check                                                            |
//| Prints one PASS or FAIL line and counts it.                      |
//+------------------------------------------------------------------+
void Check(const bool ok,const string name,const string detail="")
  {
   if(ok)
      g_pass++;
   else
      g_fail++;
   PrintFormat("%s %s%s",ok ? "PASS" : "FAIL",name,(!ok && detail!="") ? " | "+detail : "");
  }
//+------------------------------------------------------------------+
//| Rand                                                             |
//| Deterministic generator, so every run sees the same texts.       |
//+------------------------------------------------------------------+
int Rand(const int limit)
  {
   g_seed=g_seed*1664525+1013904223;
   return((int)((g_seed>>8)%(uint)limit));
  }
//+------------------------------------------------------------------+
//| Repeat                                                           |
//+------------------------------------------------------------------+
string Repeat(const string unit,const int n)
  {
   string s="";
   for(int i=0; i<n; i++)
      s+=unit;
   return(s);
  }
//+------------------------------------------------------------------+
//| RandomText                                                       |
//| Words of 1 to max_word code units. With allow_clusters, some     |
//| words consist of emoji, joined emoji, flags, skin tones or       |
//| accented letters, so that every kind of multi-unit character     |
//| falls on many positions.                                         |
//+------------------------------------------------------------------+
string RandomText(const int word_count,const int max_word,const bool allow_clusters=true)
  {
   string units[];
   ArrayResize(units,5);
   units[0]=g_emoji;
   units[1]=g_family;
   units[2]=g_flag;
   units[3]=g_thumb;
   units[4]=g_accent;
   string text="";
   for(int w=0; w<word_count; w++)
     {
      string word="";
      if(allow_clusters && Rand(4)==0)
        {
         string unit=units[Rand(5)];
         int count=1+Rand(MathMax(1,max_word/StringLen(unit)));
         word=Repeat(unit,count);
        }
      else
        {
         int len=1+Rand(max_word);
         for(int i=0; i<len; i++)
            word+=ShortToString((ushort)('a'+Rand(26)));
        }
      text+=(w>0 ? " " : "")+word;
     }
   return(text);
  }
//+------------------------------------------------------------------+
//| Squeeze                                                          |
//| The text without any space, tab or line break.                   |
//+------------------------------------------------------------------+
string Squeeze(const string text)
  {
   string t=CTextWrap::Normalize(text);
   StringReplace(t,"\n","");
   StringReplace(t," ","");
   return(t);
  }
//+------------------------------------------------------------------+
//| JoinLines                                                        |
//+------------------------------------------------------------------+
string JoinLines(const string &lines[])
  {
   string s="";
   for(int i=0; i<ArraySize(lines); i++)
      s+=lines[i];
   return(s);
  }
//+------------------------------------------------------------------+
//| IsWord                                                           |
//| True when a token is one of the words of a text.                 |
//+------------------------------------------------------------------+
bool IsWord(const string &words[],const string token)
  {
   for(int i=0; i<ArraySize(words); i++)
      if(words[i]==token)
         return(true);
   return(false);
  }
//+------------------------------------------------------------------+
//| SplitAt                                                          |
//| True when a line starts or ends inside a character. Written      |
//| apart from CTextWrap::CanCut(), so the check does not reuse the  |
//| rule it checks. Every flag word of the tests has an even number  |
//| of regional indicators, so an odd run at the end of a line means |
//| that a flag was cut.                                             |
//+------------------------------------------------------------------+
bool SplitAt(const string line)
  {
   int n=StringLen(line);
   if(n==0)
      return(false);
   ushort first=StringGetCharacter(line,0);
   ushort last=StringGetCharacter(line,n-1);
   if((first>=0xDC00 && first<=0xDFFF) || first==0x0301 || first==0x200D || first==0xFE0F)
      return(true);
   if(first==0xD83C && n>1 && StringGetCharacter(line,1)>=0xDFFB && StringGetCharacter(line,1)<=0xDFFF)
      return(true);
   if((last>=0xD800 && last<=0xDBFF) || last==0x200D)
      return(true);
   int run=0;
   for(int k=n-2; k>=0 && StringGetCharacter(line,k)==0xD83C && StringGetCharacter(line,k+1)>=0xDDE6 && StringGetCharacter(line,k+1)<=0xDDFF; k-=2)
      run++;
   return(run%2==1);
  }
//+------------------------------------------------------------------+
//| Violations                                                       |
//| Checks the lines of a wrapper against the four properties of a   |
//| correct result. A word is only reported as cut when every word   |
//| of the text is short enough to fit on a line.                    |
//+------------------------------------------------------------------+
int Violations(ITextMeter &meter,const string text,const string &lines[],const int max_px,const int max_chars)
  {
   int bad=0;
   if(Squeeze(JoinLines(lines))!=Squeeze(text))
      bad|=BAD_LOSS;
   string words[];
   StringSplit(CTextWrap::Normalize(text),' ',words);
   for(int i=0; i<ArraySize(lines); i++)
     {
      if(StringLen(lines[i])>max_chars || meter.Width(lines[i])>max_px)
         bad|=BAD_TOO_LONG;
      if(SplitAt(lines[i]))
         bad|=BAD_SPLIT;
      string tokens[];
      int count=StringSplit(lines[i],' ',tokens);
      for(int t=0; t<count; t++)
         if(tokens[t]!="" && !IsWord(words,tokens[t]))
            bad|=BAD_WORD_CUT;
     }
   return(bad);
  }
//+------------------------------------------------------------------+
//| AllEqual                                                         |
//| True when every line equals the expected text.                   |
//+------------------------------------------------------------------+
bool AllEqual(const string &lines[],const string expected)
  {
   for(int i=0; i<ArraySize(lines); i++)
      if(lines[i]!=expected)
         return(false);
   return(ArraySize(lines)>0);
  }
//+------------------------------------------------------------------+
//| NaiveSplit                                                       |
//| The obvious fix, written to be caught: cut every 63 code units.  |
//+------------------------------------------------------------------+
void NaiveSplit(const string text,string &lines[])
  {
   ArrayResize(lines,0);
   for(int pos=0; pos<StringLen(text); pos+=TXT_LABEL_LIMIT)
     {
      int n=ArraySize(lines);
      ArrayResize(lines,n+1);
      lines[n]=StringSubstr(text,pos,TXT_LABEL_LIMIT);
     }
  }
//+------------------------------------------------------------------+
//| CountObjects                                                     |
//| Number of chart objects whose name starts with a prefix.         |
//+------------------------------------------------------------------+
int CountObjects(const string prefix)
  {
   int count=0;
   for(int i=ObjectsTotal(0)-1; i>=0; i--)
      if(StringFind(ObjectName(0,i),prefix)==0)
         count++;
   return(count);
  }
//+------------------------------------------------------------------+
//| TestWrapRules                                                    |
//| Wrap rules with a meter of 10 pixels per code unit.              |
//+------------------------------------------------------------------+
void TestWrapRules()
  {
   CFixedMeter meter(10,16);
   string lines[];
   Check(CTextWrap::Wrap(meter,"",100,63,lines)==0,"wrap: empty text gives 0 lines");
   Check(CTextWrap::Wrap(meter,"hello world",1000,63,lines)==1 && lines[0]=="hello world","wrap: a short text is unchanged");
   Check(CTextWrap::Wrap(meter,"aaaa bbbb cccc",100,63,lines)==2 && lines[0]=="aaaa bbbb" && lines[1]=="cccc","wrap: the width limit breaks between words");
   Check(CTextWrap::Wrap(meter,"aaaa bbbb cccc",90,63,lines)==2 && lines[0]=="aaaa bbbb","wrap: a line may be exactly as wide as the limit");
   Check(CTextWrap::Wrap(meter,"aaaa bbbb cccc",89,63,lines)==3,"wrap: one pixel less gives one more line");
   string long_words=Repeat("abcdefghi ",12);
   int n=CTextWrap::Wrap(meter,long_words,100000,63,lines);
   bool within=true;
   for(int i=0; i<n; i++)
      within=within && StringLen(lines[i])<=63;
   Check(n==2 && within && Violations(meter,long_words,lines,100000,63)==0,"wrap: the 63 cap breaks between words when the width allows more");
   string one_word=Repeat("x",150);
   n=CTextWrap::Wrap(meter,one_word,100000,63,lines);
   Check(n==3 && StringLen(lines[0])==63 && StringLen(lines[1])==63 && StringLen(lines[2])==24,"wrap: a word of 150 code units is cut 63, 63, 24");
   n=CTextWrap::Wrap(meter,Repeat("x",25),100,63,lines);
   Check(n==3 && lines[0]==Repeat("x",10) && lines[2]==Repeat("x",5),"wrap: a word wider than the limit is cut at the longest prefix that fits");
   Check(CTextWrap::Wrap(meter,"one\ntwo",1000,63,lines)==2 && lines[1]=="two","wrap: a line break starts a new line");
   Check(CTextWrap::Wrap(meter,"a\n\nb",1000,63,lines)==3 && lines[1]=="","wrap: an empty paragraph keeps an empty line");
   Check(CTextWrap::Wrap(meter,"one\r\ntwo\rthree",1000,63,lines)==3,"wrap: CR LF and CR count as line breaks");
   Check(CTextWrap::Wrap(meter,"a  b\t\tc",1000,63,lines)==1 && lines[0]=="a b c","wrap: runs of spaces and tabs become one space");
   Check(CTextWrap::Wrap(meter,"abc",0,63,lines)==-1 && CTextWrap::Wrap(meter,"abc",100,1,lines)==-1,"wrap: invalid limits return -1");
   n=CTextWrap::Wrap(meter,"abcd",5,63,lines);
   Check(n==4 && lines[3]=="d" && CTextWrap::Overwide()==4,"wrap: a limit below one character width gives one character per line, and Overwide() counts them");
   n=CTextWrap::Wrap(meter,Repeat("a",62)+g_emoji,100000,63,lines);
   Check(n==2 && StringLen(lines[0])==62 && lines[1]==g_emoji,"wrap: a surrogate pair at the cap moves to the next line");
   n=CTextWrap::Wrap(meter,g_emoji+g_emoji,10,63,lines);
   Check(n==2 && lines[0]==g_emoji && lines[1]==g_emoji && CTextWrap::Overwide()==2,"wrap: a pair wider than the limit stays whole, and Overwide() reports both lines");
   n=CTextWrap::Wrap(meter,Repeat(g_accent,10),55,63,lines);
   Check(n==5 && AllEqual(lines,g_accent+g_accent),"wrap: a combining accent stays with its letter (5 units fit, the cut is after 4)");
   n=CTextWrap::Wrap(meter,Repeat(g_family,3),100,63,lines);
   Check(n==3 && AllEqual(lines,g_family),"wrap: an emoji sequence joined by zero width joiners is not cut");
   n=CTextWrap::Wrap(meter,Repeat(g_flag,5),70,63,lines);
   Check(n==5 && AllEqual(lines,g_flag),"wrap: a flag of two regional indicators is not cut (7 units fit, the cut is after 4)");
   n=CTextWrap::Wrap(meter,Repeat(g_thumb,5),70,63,lines);
   Check(n==5 && AllEqual(lines,g_thumb),"wrap: an emoji with a skin tone modifier is not cut");
   string bold_a=ShortToString(0xD835)+ShortToString(0xDC00);
   string word="x"+bold_a+"y";
   Check(CTextWrap::CanCut(word,1) && !CTextWrap::CanCut(word,2) && CTextWrap::CanCut(word,3),
         "cut: CanCut() refuses only the position between the two halves of a plain surrogate pair (U+1D400)");
   n=CTextWrap::Wrap(meter,Repeat("a",62)+bold_a,100000,63,lines);
   Check(n==2 && StringLen(lines[0])==62 && lines[1]==bold_a,"wrap: a plain surrogate pair (U+1D400) at the cap moves to the next line");
  }
//+------------------------------------------------------------------+
//| TestWrapProperties                                               |
//| Random texts against the four properties of a correct result.    |
//+------------------------------------------------------------------+
void TestWrapProperties()
  {
   CFixedMeter meter(7,16);
   int bad_all=0,bad_greedy=0;
   for(int k=0; k<InpRandomTexts; k++)
     {
      string text=RandomText(5+Rand(60),12);
      int max_px=7*(20+Rand(80));
      string lines[];
      CTextWrap::Wrap(meter,text,max_px,TXT_LABEL_LIMIT,lines);
      if(Violations(meter,text,lines,max_px,TXT_LABEL_LIMIT)!=0)
         bad_all++;
      for(int i=0; i+1<ArraySize(lines); i++)
        {
         string next[];
         StringSplit(lines[i+1],' ',next);
         string grown=lines[i]+" "+next[0];
         if(StringLen(grown)<=TXT_LABEL_LIMIT && meter.Width(grown)<=max_px && StringFind(text,lines[i]+" "+next[0])>=0)
            bad_greedy++;
        }
     }
   Check(bad_all==0,StringFormat("properties: %d random texts keep every character except whitespace, stay within both limits and cut no character",
                                 InpRandomTexts),StringFormat("%d texts violated a property",bad_all));
   Check(bad_greedy==0,"properties: no line could have taken the first word of the next line","greedy breaks: "+IntegerToString(bad_greedy));
//--- a meter whose width drops at every fourth code unit
   CBumpyMeter bumpy;
   int bumpy_bad=0,bumpy_over=0;
   for(int k=0; k<InpRandomTexts; k++)
     {
      string text=RandomText(3+Rand(20),40,false);
      int max_px=60+Rand(300);
      string lines[];
      CTextWrap::Wrap(bumpy,text,max_px,TXT_LABEL_LIMIT,lines);
      bumpy_over+=CTextWrap::Overwide();
      if((Violations(bumpy,text,lines,max_px,TXT_LABEL_LIMIT)&(BAD_TOO_LONG|BAD_LOSS))!=0)
         bumpy_bad++;
     }
   Check(bumpy_bad==0 && bumpy_over==0,StringFormat("properties: with a meter that gets narrower at every fourth unit, %d random texts keep every character and no line exceeds a limit",
                                                    InpRandomTexts),StringFormat("bad %d, overwide %d",bumpy_bad,bumpy_over));
//--- mutation checks: wrong results that the properties must report
   int found=0;
   for(int k=0; k<InpRandomTexts; k++)
     {
      string text=RandomText(8+Rand(40),10);
      string naive[];
      NaiveSplit(text,naive);
      if(Violations(meter,text,naive,100000,TXT_LABEL_LIMIT)!=0)
         found++;
     }
   Check(found>InpRandomTexts/2,"mutation: the cut-every-63 splitter is flagged on most texts (expected to fail the properties)",
         StringFormat("flagged %d of %d",found,InpRandomTexts));
   string probe=Repeat("a",62)+g_emoji+" tail";
   string naive_lines[];
   NaiveSplit(probe,naive_lines);
   Check((Violations(meter,probe,naive_lines,100000,TXT_LABEL_LIMIT)&BAD_SPLIT)!=0,"mutation: the cut-every-63 splitter splits a surrogate pair and the check reports it");
   string capless[];
   CTextWrap::Wrap(meter,Repeat("word ",60),100000,TXT_NO_LIMIT,capless);
   Check((Violations(meter,Repeat("word ",60),capless,100000,TXT_LABEL_LIMIT)&BAD_TOO_LONG)!=0,"mutation: wrapping without the 63 cap is reported as too long");
  }
//+------------------------------------------------------------------+
//| TestScreenMeter                                                  |
//| The real fonts, on the screen of this terminal.                  |
//+------------------------------------------------------------------+
void TestScreenMeter()
  {
   string fonts[]={"Arial","Times New Roman","Consolas","Segoe UI","Calibri","Tahoma","Verdana"};
   string samples[]={"WWWWWWWWWW","Illumination","status: 12 positions, margin level 487.35%","0123456789012345678901234567890",
                     "AVAVAV To Ty Yo LT Wa Te","office fluffy affine","Equity 10482.19, margin 487.35%, spread 0.4, positions 3"};
   int arial_dips=0;
   for(int f=0; f<ArraySize(fonts); f++)
     {
      CScreenMeter font_meter;
      font_meter.Set(fonts[f],10);
      int dips=0,prefixes=0;
      for(int s=0; s<ArraySize(samples); s++)
         for(int k=2; k<=StringLen(samples[s]); k++)
           {
            prefixes++;
            if(font_meter.Width(StringSubstr(samples[s],0,k))<font_meter.Width(StringSubstr(samples[s],0,k-1)))
               dips++;
           }
      PrintFormat("MONO font=%s points=10 prefixes=%d narrower_than_shorter=%d",fonts[f],prefixes,dips);
      if(f==0)
         arial_dips=dips;
     }
   Check(arial_dips==0,"meter: with Arial the width never shrinks when a character is added (the binary search then finds the longest prefix)",
         "dips: "+IntegerToString(arial_dips));
   CScreenMeter meter;
   meter.Set("Arial",10);
   Check(meter.Width("WWWWWWWWWW")>meter.Width("iiiiiiiiii"),"meter: a proportional font gives W wider than i");
   Check(meter.LineHeight()>0,"meter: line height is positive");
   string lines[];
   int bad=0;
   for(int k=0; k<60; k++)
     {
      string text=RandomText(10+Rand(50),11,false);
      int max_px=meter.Width("WWWWWWWWWWWW")*(1+Rand(4));
      int n=CTextWrap::Wrap(meter,text,max_px,TXT_LABEL_LIMIT,lines);
      if(n<1 || Violations(meter,text,lines,max_px,TXT_LABEL_LIMIT)!=0)
         bad++;
     }
   Check(bad==0,"meter: 60 random texts wrapped with the screen font keep every property","violations: "+IntegerToString(bad));
  }
//+------------------------------------------------------------------+
//| TestLabelBlock                                                   |
//| The label block on the chart of the script.                      |
//+------------------------------------------------------------------+
void TestLabelBlock()
  {
   CLabelBlock refused;
   Check(!refused.Init(Repeat("p",60),10,10,400),"labels: a prefix that makes a 64-unit name is refused");
   Check(refused.Set("x")==TXT_ERR_NOT_READY,"labels: a block that is not ready returns -1");
   CLabelBlock ok_prefix;
   Check(ok_prefix.Init(Repeat("p",59),10,10,400) && ok_prefix.Set("x")==1,"labels: a prefix that makes a 63-unit name is accepted");
   ok_prefix.Clear();
   Check(CountObjects(Repeat("p",59))==0,"labels: Clear() deletes the labels");
   string leftovers[]={"CTT_old_000","CTT_old_001","CTT_old_005","CTT_old_017"};
   for(int i=0; i<ArraySize(leftovers); i++)
      ObjectCreate(0,leftovers[i],OBJ_LABEL,0,0,0);
   ObjectCreate(0,"CTT_old_x",OBJ_LABEL,0,0,0);
   ObjectCreate(0,"CTT_oldest_000",OBJ_LABEL,0,0,0);
   CLabelBlock block;
   bool init_ok=block.Init("CTT_old",10,10,420);
   bool gone=true;
   for(int i=0; i<ArraySize(leftovers); i++)
      gone=gone && ObjectFind(0,leftovers[i])<0;
   Check(init_ok && gone,"labels: Init() deletes leftovers of the same prefix, also when their numbers have gaps (000, 001, 005, 017)");
   Check(ObjectFind(0,"CTT_old_x")>=0 && ObjectFind(0,"CTT_oldest_000")>=0,"labels: Init() keeps objects whose names only start like the prefix");
   ObjectDelete(0,"CTT_old_x");
   ObjectDelete(0,"CTT_oldest_000");
   string text=Repeat("The status line of this expert is much longer than a label can hold, ",16);
   int n=block.Set(text);
   Check(n>=20,"labels: a text of "+IntegerToString(StringLen(text))+" code units needs several labels","lines: "+IntegerToString(n));
   Check(CountObjects("CTT_old")==n,"labels: one object per line");
   bool within=true;
   string joined="";
   for(int i=0; i<n; i++)
     {
      string stored=ObjectGetString(0,block.NameAt(i),OBJPROP_TEXT);
      within=within && StringLen(stored)<=TXT_LABEL_LIMIT;
      joined+=stored;
     }
   Check(within,"labels: every stored text is at most 63 code units");
   Check(Squeeze(joined)==Squeeze(text),"labels: the stored lines hold every character of the text except whitespace, in order");
   Check(block.Verify()==0,"labels: Verify() finds no difference after Set()");
   int calls=block.ObjectCalls();
   Check(block.Set(text)==n && block.ObjectCalls()==calls,"labels: the same text again makes no object call");
   string changed=text;
   StringSetCharacter(changed,5,'X');
   block.Set(changed);
   Check(block.ObjectCalls()-calls==1,"labels: a change that keeps every line break updates one label","calls: "+IntegerToString(block.ObjectCalls()-calls));
   calls=block.ObjectCalls();
   block.Set("WWWWWWWWWWWW "+changed);
   int moved=block.ObjectCalls()-calls;
   Check(moved>1,"labels: a change that moves a line break updates more than one label","calls: "+IntegerToString(moved));
   PrintFormat("LABELS one word added at the start: %d object calls for %d lines",moved,block.LineCount());
   int shorter=block.Set("A short text.");
   Check(shorter==1 && CountObjects("CTT_old")==1 && ObjectFind(0,block.NameAt(n-1))<0,"labels: a shorter text deletes the surplus labels");
   ObjectSetString(0,block.NameAt(0),OBJPROP_TEXT,Repeat("z",70));
   Check(block.Verify()==1,"labels: Verify() reports a label that was set outside the block (70 units stored as 63)");
   Check(block.Set("A short text.")==1 && block.Verify()==1,"labels: the same text is not written again, so an outside change stays until Clear()");
   block.Clear();
   block.Set("A short text.");
   Check(block.Verify()==0,"labels: Clear() followed by Set() writes the lines again");
   ObjectDelete(0,block.NameAt(0));
   Check(block.Verify()==1,"labels: Verify() reports a label that was deleted outside the block");
   string many=Repeat("alpha ",1100);
   CLabelBlock crowded;
   crowded.Init("CTT_many",10,10,60);
   int crowded_result=crowded.Set(many);
   string last=ObjectGetString(0,crowded.NameAt(TXT_MAX_LINES-1),OBJPROP_TEXT);
   Check(crowded_result==TXT_ERR_TOO_LONG && CountObjects("CTT_many")==TXT_MAX_LINES && StringFind(last,"more lines not shown")>0 && crowded.Verify()==0,
         "labels: a text of 1,100 lines returns -3 and shows 998 lines and one line that says how many are missing","last line: "+last);
   PrintFormat("LABELS too long: result=%d labels=%d last=%s",crowded_result,CountObjects("CTT_many"),last);
   int repeat_calls=crowded.ObjectCalls();
   int repeat_result=crowded.Set(many);
   Check(repeat_result==TXT_ERR_TOO_LONG && crowded.ObjectCalls()==repeat_calls,"labels: the same too long text again returns -3 and makes no object call",
         StringFormat("result %d",repeat_result));
   PrintFormat("LABELS too long again: result=%d object_calls=%d",repeat_result,crowded.ObjectCalls()-repeat_calls);
   crowded.Clear();
   CLabelBlock nowhere;
   nowhere.Init("CTT_nowhere",10,10,400,"Arial",10,clrBlack,2,1);
   int nowhere_result=nowhere.Set("text for a chart that does not exist");
   Check(nowhere_result==TXT_ERR_CHART && nowhere.FailedCalls()>0 && nowhere.LastError()!=0,"labels: a failed chart call returns -2 and keeps its error code",
         StringFormat("result %d, failed %d, error %d",nowhere_result,nowhere.FailedCalls(),nowhere.LastError()));
   PrintFormat("LABELS chart that does not exist: result=%d failed_calls=%d last_error=%d",nowhere_result,nowhere.FailedCalls(),nowhere.LastError());
   block.Clear();
   Check(CountObjects("CTT_old")==0 && CountObjects("CTT_many")==0 && block.LineCount()==0,"labels: Clear() leaves no label");
  }
//+------------------------------------------------------------------+
//| TestCanvasBlock                                                  |
//| The canvas block on the chart of the script.                     |
//+------------------------------------------------------------------+
void TestCanvasBlock()
  {
   CCanvasBlock refused;
   Check(!refused.Init(Repeat("c",64),10,10,400),"canvas: a name of 64 code units is refused");
   CCanvasBlock block;
   Check(block.Init("CTT_canvas",10,10,1200),"canvas: Init() accepts a short name");
   string text=Repeat("The status line of this expert is much longer than a label can hold, ",16);
   int n=block.Set(text);
   Check(n>=1 && ObjectFind(0,"CTT_canvas")>=0,"canvas: Set() creates one bitmap label","lines: "+IntegerToString(n));
   Check(block.Verify()==0,"canvas: Verify() finds every line within the width and with ink in the bitmap");
   Check(block.LineCount()==n,"canvas: LineCount() matches Set()");
   CScreenMeter meter;
   string lines[];
   int plain=CTextWrap::Wrap(meter,text,1200,TXT_NO_LIMIT,lines);
   bool longer=false;
   for(int i=0; i<plain; i++)
      longer=longer || StringLen(lines[i])>TXT_LABEL_LIMIT;
   Check(plain==n && longer,"canvas: with 1200 pixels a line holds more than 63 code units");
   int height=block.Height();
   block.Set(text);
   Check(block.Height()==height,"canvas: the same text keeps the bitmap size");
   Check(block.Set("")==0 && ObjectFind(0,"CTT_canvas")<0,"canvas: an empty text removes the bitmap label");
   block.Set("again");
   block.Clear();
   Check(ObjectFind(0,"CTT_canvas")<0,"canvas: Clear() removes the bitmap label");
   CCanvasBlock invisible;
   invisible.Init("CTT_invisible",10,10,600,"Arial",10,clrWhite,clrWhite);
   int shown=invisible.Set(text);
   Check(shown>0 && invisible.Verify()==shown,"mutation: text drawn in the background color leaves no ink, and Verify() reports every line",
         StringFormat("lines %d, verify %d",shown,invisible.Verify()));
   invisible.Clear();
   CCanvasBlock crowded;
   crowded.Init("CTT_canvas_many",10,10,60);
   int crowded_result=crowded.Set(Repeat("alpha ",1100));
   Check(crowded_result==TXT_ERR_TOO_LONG && crowded.LineCount()==TXT_MAX_LINES,"canvas: a text of 1,100 lines returns -3 and draws 999 lines",
         StringFormat("result %d, lines %d",crowded_result,crowded.LineCount()));
   PrintFormat("CANVAS too long: result=%d lines=%d bitmap_height=%d",crowded_result,crowded.LineCount(),crowded.Height());
   int repeat_result=crowded.Set(Repeat("alpha ",1100));
   Check(repeat_result==TXT_ERR_TOO_LONG,"canvas: the same too long text again returns -3",StringFormat("result %d",repeat_result));
   PrintFormat("CANVAS too long again: result=%d",repeat_result);
   crowded.Clear();
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   g_emoji=ShortToString(0xD83D)+ShortToString(0xDE00);
   g_family=ShortToString(0xD83D)+ShortToString(0xDC68)+ShortToString(0x200D)+ShortToString(0xD83D)+ShortToString(0xDC69)+
            ShortToString(0x200D)+ShortToString(0xD83D)+ShortToString(0xDC67);
   g_flag=ShortToString(0xD83C)+ShortToString(0xDDF5)+ShortToString(0xD83C)+ShortToString(0xDDF0);
   g_thumb=ShortToString(0xD83D)+ShortToString(0xDC4D)+ShortToString(0xD83C)+ShortToString(0xDFFD);
   g_accent="e"+ShortToString(0x0301);
   PrintFormat("TESTS build=%d dpi=%d",TerminalInfoInteger(TERMINAL_BUILD),TerminalInfoInteger(TERMINAL_SCREEN_DPI));
   TestWrapRules();
   TestWrapProperties();
   TestScreenMeter();
   TestLabelBlock();
   TestCanvasBlock();
   PrintFormat("TESTS %d passed, %d failed",g_pass,g_fail);
  }
//+------------------------------------------------------------------+
