// TestControl.m
//
// by Mike Ferris
// Part of MOKit
// Copyright 1993, all rights reserved.


// ABOUT MOKit
// by Mike Ferris (mike@lorax.com)
//
// MOKit is a collection of useful and general objects.  Permission is 
// granted by the author to use MOKit in your own programs in any way 
// you see fit.  All other rights pertaining to the kit are reserved by the 
// author including the right to sell these objects as objects,  as part 
// of a LIBRARY, or as SOURCE CODE.  In plain English, I wish to retain 
// rights to these objects as objects, but allow the use of the objects 
// as pieces in a fully functional program.  Permission is also granted to 
// redistribute the source code of MOKit for FREE as long as this copyright 
// notice is left intact and unchanged.  NO WARRANTY is expressed or implied.  
// The author will under no circumstances be held responsible for ANY 
// consequences from the use of these objects.  Since you don't have to pay 
// for them, and full source is provided, I think this is perfectly fair.

#import "TestControl.h"
#import "MOKit/MOString.h"
#import "MOKit/MOSybaseString.h"
#import "MOKit/MOPathString.h"

@implementation TestControl

- init
{
	[super init];
	
	return self;
}

- appDidInit:sender
{
	return self;
}

- appendText:(const char *)newText
// add newText to the end of the text object
{
	if (newText)  {
		if (outputText)  {
			int currentLength = [outputText textLength];
			[outputText setSel:currentLength :currentLength];
			[outputText replaceSel:newText];
			[outputText scrollSelToVisible];
		}  else  {
			NXLogError(newText);
		}
	}
	return self;
}

- appendString:string
{
	if (string)  {
		[self appendText:[string stringValue]];
	}
	return self;
}

- doEndOfSection
{
	return [self appendText:"\n\n------------------------------\n\n"];
}

- parseTest
{
	id str1, str2;
	id outBuff;
	id tokenList;
	int i, c;

	str1 = [[MOString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];

	[self appendText:"Parse test:\n"];
	
	[str1 setStringValue:"ARMS, and the man I sing, who, forc'd by fate,"];

	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n\n", [str1 stringValue]];
	[self appendString:outBuff];
	
	[outBuff setFromFormat:"[str1 charAt:3] = %c\n", [str1 charAt:3]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 charAt:14] = %c\n", [str1 charAt:14]];
	[self appendString:outBuff];
	
	str2 = [str1 substringFrom:0 to:4];
	[outBuff setFromFormat:"[str1 substringFrom:0 to:4] = \"%s\"\n", 
				[str2 stringValue]];
	[self appendString:outBuff];
	[str2 free];
	
	str2 = [str1 substringFrom:31 to:44];
	[outBuff setFromFormat:"[str1 substringFrom:31 to:44] = \"%s\"\n", 
				[str2 stringValue]];
	[self appendString:outBuff];
	[str2 free];
	
	[outBuff setFromFormat:"[str1 positionOf:'n' nthOccurrence:1] = %d\n", 
				[str1 positionOf:'n' nthOccurrence:1]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 positionOf:'n' nthOccurrence:2] = %d\n", 
				[str1 positionOf:'n' nthOccurrence:2]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 positionOf:'n' nthOccurrence:-1] = %d\n", 
				[str1 positionOf:'n' nthOccurrence:-1]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 positionOf:'n' nthOccurrence:-2] = %d\n", 
				[str1 positionOf:'n' nthOccurrence:-2]];
	[self appendString:outBuff];
	
	[self appendText:"Tokenizing str1.  Break at spaces.\n"];
	tokenList = [str1 tokenize:" " into:nil];
	if (tokenList)  {
		c = [tokenList count];
		for (i=0; i<c; i++)  {
			[outBuff setFromFormat:"\tToken %d = \"%s\"\n", i, 
						[[tokenList objectAt:i] stringValue]];
			[self appendString:outBuff];
		}
		[[tokenList freeObjects] free];
	}  else  {
		[self appendText:"No tokens.\n"];
	}
	
	[self doEndOfSection];
	
	[str1 free];
	[outBuff free];
	
	return self;
}

- modifyTest
{
	id str1, str2;
	id outBuff;

	str1 = [[MOString allocFromZone:[self zone]] init];
	str2 = [[MOString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];

	[self appendText:"Modification test:\n"];
	
	[str1 setStringValue:"xyzzy"];

	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n\n", [str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 replaceCharAt:0 with:'q'];
	[outBuff setFromFormat:"[str1 replaceCharAt:0 with:'q'] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];

	[str1 replaceAllOccurrencesOfChar:'y' with:'p'];
	[outBuff setFromFormat:"[str1 replaceAllOccurrencesOfChar:'y' with:'p'] "
				"= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];

	[str1 convertToUpper];
	[outBuff setFromFormat:"[str1 convertToUpper] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];

	[str1 convertToLower];
	[outBuff setFromFormat:"[str1 convertToLower] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str2 setStringValue:"Hello"];
	[outBuff setFromFormat:"\n\tstring 2 = \"%s\"\n\n", [str2 stringValue]];
	[self appendString:outBuff];
	[str1 takeStringValueFrom:str2];
	[outBuff setFromFormat:"[str1 takeStringValueFrom:str2] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str2 setIntValue:12];
	[outBuff setFromFormat:"\n\tstring 2 = \"%s\"\n\n", [str2 stringValue]];
	[self appendString:outBuff];
	[str1 takeIntValueFrom:str2];
	[outBuff setFromFormat:"[str1 takeIntValueFrom:str2] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str2 setFloatValue:12.24];
	[outBuff setFromFormat:"\n\tstring 2 = \"%s\"\n\n", [str2 stringValue]];
	[self appendString:outBuff];
	[str1 takeFloatValueFrom:str2];
	[outBuff setFromFormat:"[str1 takeFloatValueFrom:str2] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 takeDoubleValueFrom:str2];
	[outBuff setFromFormat:"[str1 takeDoubleValueFrom:str2] = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[self doEndOfSection];
	
	[str1 free];  [str2 free];
	[outBuff free];
	
	return self;
}

- addTest
{
	id str1, str2, str3;
	id outBuff;

	str1 = [[MOString allocFromZone:[self zone]] init];
	str2 = [[MOString allocFromZone:[self zone]] init];
	str3 = [[MOString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];

	[self appendText:"cat..., preCat..., insert... test:\n"];
	
	[str1 setStringValue:"is the good men to come"];
	[str2 setStringValue:"time for all "];
	
	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n", [str1 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 2 = \"%s\"\n", [str2 stringValue]];
	[self appendString:outBuff];
	
	[str3 setStringValue:" to the aid"];
	[outBuff setFromFormat:"\tstring 3 = \"%s\"\n\n", [str3 stringValue]];
	[self appendString:outBuff];
	[str1 cat:str3];
	[outBuff setFromFormat:"[str1 cat:str3] = \"%s\"\n", [str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 catStringValue:" of"];
	[outBuff setFromFormat:"[str1 catStringValue:\" of\"] \n\t= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 catFromFormat:" %s %s", "their", "country"];
	[outBuff setFromFormat:"[str1 catFromFormat:\" %%s %%s\", \"their\", "
				"\"country\"] \n\t= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 insert:str2 at:7];
	[outBuff setFromFormat:"[str1 insert:str2 at:7] \n\t= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str3 setStringValue:"Now "];
	[outBuff setFromFormat:"\n\tstring 3 = \"%s\"\n\n", [str3 stringValue]];
	[self appendString:outBuff];
	[str1 preCat:str3];
	[outBuff setFromFormat:"[str1 preCat:str3] \n\t= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 insertStringValue:"really " at:7];
	[outBuff setFromFormat:"[str1 insertStringValue:\"really \" at:7] \n\t= "
				"\"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 preCatStringValue:"Verily, "];
	[outBuff setFromFormat:"[str1 preCatStringValue:\"Verily, \"] \n\t= "
				"\"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[str1 preCatFromFormat:"%s %s, ", "Oh", "Baby"];
	[outBuff setFromFormat:"[str1 preCatFromFormat:\"%%s %%s, \", "
				"\"Oh\", \"Baby\"] \n\t= \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	
	[self doEndOfSection];
	
	[str1 free]; [str2 free]; [str3 free];
	[outBuff free];
	
	return self;
}

- compareTest
{
	id str1, str2, str3;
	id outBuff;

	str1 = [[MOString allocFromZone:[self zone]] init];
	str2 = [[MOString allocFromZone:[self zone]] init];
	str3 = [[MOString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];

	[self appendText:"Comparison test:\n"];
	
	[str1 setStringValue:"We the people, in order to find..."];
	[str2 setStringValue:"We the people, In order to find..."];
	[str3 setStringValue:"We the stupid horse-brained masses... "];
	
	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n", [str1 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 2 = \"%s\"\n", [str2 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 3 = \"%s\"\n\n", [str3 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 compare:str2] = %d\n", 
				[str1 compare:str2]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 compare:str2 caseSensitive:NO] = %d\n", 
				[str1 compare:str2 caseSensitive:NO]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 compare:str3 caseSensitive:NO] = %d\n", 
				[str1 compare:str3 caseSensitive:NO]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 compare:str3 caseSensitive:NO length:6] "
				"= %d\n", 
				[str1 compare:str3 caseSensitive:NO length:6]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 compareStr:\"WE THE PEOPLE\" "
				"caseSensitive:NO length:13] = %d\n", 
				[str1 compareStr:"WE THE PEOPLE" caseSensitive:NO length:13]];
	[self appendString:outBuff];

	
	[self doEndOfSection];
	
	[str1 free]; [str2 free]; [str3 free];
	[outBuff free];
	
	return self;
}

- endCompareTest
{
	id str1, str2, str3;
	id outBuff;

	str1 = [[MOString allocFromZone:[self zone]] init];
	str2 = [[MOString allocFromZone:[self zone]] init];
	str3 = [[MOString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];

	[self appendText:"End comparison test:\n"];
	
	[str1 setStringValue:"HelloThere.rtf"];
	[str2 setStringValue:"SomeFile.rtf"];
	[str3 setStringValue:".rtf"];
	
	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n", [str1 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 2 = \"%s\"\n", [str2 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 3 = \"%s\"\n\n", [str3 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 endCompare:str3] = %d\n", 
				[str1 endCompare:str3]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 endCompare:str2 caseSensitive:NO "
				"length:4] = %d\n", 
				[str1 endCompare:str2 caseSensitive:NO length:4]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 endCompare:str2 caseSensitive:NO "
				"length:6] = %d\n", 
				[str1 endCompare:str2 caseSensitive:NO length:6]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str2 endCompareStr:\".rtf\"] = %d\n", 
				[str2 endCompareStr:".rtf"]];
	[self appendString:outBuff];
	
	[self doEndOfSection];
	
	[str1 free]; [str2 free]; [str3 free];
	[outBuff free];
	
	return self;
}

- runMOStringDiagnostic:sender
{
//	id str1, str2, str3;
//	id outBuff;
	
//	str1 = [[MOString allocFromZone:[self zone]] init];
//	str2 = [[MOString allocFromZone:[self zone]] init];
//	str3 = [[MOString allocFromZone:[self zone]] init];
//	outBuff = [[MOString allocFromZone:[self zone]] init];
	
	[self appendText:"MOString Diagnostic:\n"
					 "--------------------\n\n"];
	[self parseTest];
	[self modifyTest];
	[self addTest];
	[self compareTest];
	[self endCompareTest];
	
    return self;
}

- runMOSybaseStringDiagnostic:sender
{
	id str1, str2;
	id outBuff;
	
	str1 = [[MOSybaseString allocFromZone:[self zone]] init];
	str2 = [[MOSybaseString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];
	
	[self appendText:"MOSybaseString Diagnostic:\n\n"];
	
	[str1 setStringValue:"This12 String@.,will be caSE inseNsitive"];
	[str2 setStringValue:"Wildcards: *?*?*? *?"];
	
	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n", [str1 stringValue]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 2 = \"%s\"\n\n", [str2 stringValue]];
	[self appendString:outBuff];
	
	[str1 convertToCaseInsensitiveSearchString];
	[outBuff setFromFormat:"Case converted string 1 = \"%s\"\n", 
				[str1 stringValue]];
	[self appendString:outBuff];
	[str2 convertUnixWildcardsToSybase];
	[outBuff setFromFormat:"Wildcard converted string 2 = \"%s\"\n", 
				[str2 stringValue]];
	[self appendString:outBuff];
	
	[self doEndOfSection];
	
	[str1 free]; [str2 free];
	[outBuff free];
	
	return self;
}

- runMOPathStringDiagnostic:sender
{
	MOPathString *str1, *str2, *str3, *str4, *str5, *str6;
	id outBuff;
	int i, c;
	
	str1 = [[MOPathString allocFromZone:[self zone]] init];
	str2 = [[MOPathString allocFromZone:[self zone]] init];
	outBuff = [[MOString allocFromZone:[self zone]] init];
	
	[self appendText:"MOPathString Diagnostic:\n\n"];
	[self appendText:"\tThis won't fully test things if you don't have "
				"the developer docs on line since it depends on a "
				"certain file being there to test certain features.\n\n"];
	
	[str1 setPath:"/NextLibrary/Documentation/NextDev/GeneralRef/"
				"02_ApplicationKit/Protocols/NXDraggingSource.rtf"];
	[str2 setPath:"..//./.temp/blah.tiff"];
	
	[outBuff setFromFormat:"\tstring 1 = \"%s\"\n", [str1 path]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"\tstring 2 = \"%s\"\n\n", [str2 path]];
	[self appendString:outBuff];
	
	[outBuff setFromFormat:"[str1 isAbsolute] = %s, [str1 isRelative] = %s\n", 
				([str1 isAbsolute]?"YES":"NO"), 
				([str1 isRelative]?"YES":"NO")];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str2 isAbsolute] = %s, [str2 isRelative] = %s\n", 
				([str2 isAbsolute]?"YES":"NO"), 
				([str2 isRelative]?"YES":"NO")];
	[self appendString:outBuff];

	[outBuff setFromFormat:"[str1 doesExistInFileSystem] = %s\n", 
				([str1 doesExistInFileSystem]?"YES":"NO")];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str2 doesExistInFileSystem] = %s\n", 
				([str2 doesExistInFileSystem]?"YES":"NO")];
	[self appendString:outBuff];
	
	[outBuff setFromFormat:"[str1 isDirectory] = %s, "
				"[str1 isPlainFile] = %s\n", 
				([str1 isDirectory]?"YES":"NO"), 
				([str1 isPlainFile]?"YES":"NO")];
	[self appendString:outBuff];

	str3 = [str1 directory];
	str4 = [str1 file];
	[outBuff setFromFormat:"[str1 directory] = \"%s\"\n", [str3 path]];
	[self appendString:outBuff];
	[outBuff setFromFormat:"[str1 file] = \"%s\"\n", [str4 path]];
	[self appendString:outBuff];

	[outBuff setFromFormat:"[[str1 directory] isDirectory] = %s, "
				"[[str1 directory] isPlainFile] = %s\n", 
				([str3 isDirectory]?"YES":"NO"), 
				([str3 isPlainFile]?"YES":"NO")];
	[self appendString:outBuff];
	
	c = [str1 numberOfComponents];
	[outBuff setFromFormat:"String 1 has %d path components.\n", c];
	[self appendString:outBuff];
	for (i=0; i<c; i++)  {
		str5 = [str1 componentAt:i];
		[outBuff setFromFormat:"\tComponent %d = \"%s\"\n", i, [str5 path]];
		[self appendString:outBuff];
		[str5 free];
	}

	c = [str2 numberOfComponents];
	[outBuff setFromFormat:"String 2 has %d path components.\n", c];
	[self appendString:outBuff];
	for (i=0; i<c; i++)  {
		str5 = [str2 componentAt:i];
		[outBuff setFromFormat:"\tComponent %d = \"%s\"\n", i, [str5 path]];
		[self appendString:outBuff];
		[str5 free];
	}
	
	str5 = [str1 fileExtension];
	[outBuff setFromFormat:"String 1 has file extension \"%s\"\n", 
				[str5 path]];
	[self appendString:outBuff];
	[str5 free];
	
	str5 = [str1 fileBasename];
	[outBuff setFromFormat:"String 1 has file basename \"%s\"\n", 
				[str5 path]];
	[self appendString:outBuff];
	[str5 free];
	
	str5 = [str2 fileExtension];
	[outBuff setFromFormat:"String 2 has file extension \"%s\"\n", 
				[str5 path]];
	[self appendString:outBuff];
	[str5 free];

	str5 = [str2 fileBasename];
	[outBuff setFromFormat:"String 2 has file basename \"%s\"\n", 
				[str5 path]];
	[self appendString:outBuff];
	[str5 free];
	
	str5 = [str2 componentAt:0];

	str6 = [str5 fileExtension];
	[outBuff setFromFormat:"\"..\" has file extension \"%s\"\n", 
				[str6 path]];
	[self appendString:outBuff];
	[str6 free];

	str6 = [str5 fileBasename];
	[outBuff setFromFormat:"\"..\" has file basename \"%s\"\n", 
				[str6 path]];
	[self appendString:outBuff];
	[str6 free];
	
	[str5 free];
	
	str5 = [str2 componentAt:3];

	str6 = [str5 fileExtension];
	[outBuff setFromFormat:"\".temp\" has file extension \"%s\"\n", 
				[str6 path]];
	[self appendString:outBuff];
	[str6 free];

	str6 = [str5 fileBasename];
	[outBuff setFromFormat:"\".temp\" has file basename \"%s\"\n", 
				[str6 path]];
	[self appendString:outBuff];
	[str6 free];
	
	[str5 free];

	[self doEndOfSection];
	
	[str1 free]; [str2 free]; [str3 free]; [str4 free];
	[outBuff free];
	
	return self;
}

@end
