//
//  GSPhotoFontTests.m
//  GSPhotoFont
//
//  Created by Georg Seifert on 01.08.10.
//  Copyright 2010 schriftgestaltung.de. All rights reserved.
//

#import "GSPhotoFontTests.h"

@implementation GSPhotoFontTests

- (void) testBase64 {
	NSString *Text = @"\0\0this is a Test Text è and this is some more";
	NSData *Data = [NSData dataWithBytes:[Text cStringUsingEncoding:NSUTF8StringEncoding] length:[Text length]];
	//NSLog(@"  Data: %@", Data);
	NSString *Encoded = [Data encodeBase64];
	NSData *ReData = [Encoded decodeBase64];
	//NSLog(@"ReData: %@", ReData);
	STAssertTrue([Data isEqualToData: ReData], @"Data not equal");
}

- (void) testBase64PNG {
	NSString *Text = @"[encoded data, 191913 characters]";
	NSLog(@"Text: %d . %d", [Text length], [Text length] % 4);
	NSData *Data = [Text decodeBase64WithNewlines:NO];
	NSLog(@"Data: %d . %d", [Data length], [Data length] % 3);
	NSRange DataRange;
	DataRange = NSMakeRange([Data length] -20, 20);
	NSLog(@"decoded Data  in [%@]: %@", NSStringFromRange(DataRange), [Data subdataWithRange:DataRange]);
	[Data writeToFile:[@"~/Desktop/TestImage.png" stringByExpandingTildeInPath] atomically:YES];

	Data = [NSData dataWithContentsOfFile:[@"~/Desktop/TestImage.png" stringByExpandingTildeInPath]];
	DataRange = NSMakeRange([Data length] -20, 20);
	NSLog(@"readin Data  in [%@]: %@", NSStringFromRange(DataRange), [Data subdataWithRange:DataRange]);

	NSString *Encoded = [Data encodeBase64WithNewlines:NO];
	NSLog(@"Text  in: %d", [Text length]);
	NSLog(@"Text out: %d", [Encoded length]);

	int i;
	for (i = 0; i < [Text length]; i += 100) {
		NSRange TextRange = NSMakeRange(i, 100);
		if (TextRange.location + TextRange.length > [Text length]) {
			TextRange.length = [Text length] - i;
		}
		NSRange EncodeRange = NSMakeRange(i, 100);
		if (EncodeRange.location + EncodeRange.length > [Encoded length]) {
			EncodeRange.length = [Encoded length] - i;
		}

		if (![[Text substringWithRange:TextRange] isEqualToString:[Encoded substringWithRange:EncodeRange]]) {
			NSLog(@"Text  in [%@]: %@", NSStringFromRange(TextRange), [Text substringWithRange:TextRange]);
			NSLog(@"Text out[%@]: %@", NSStringFromRange(EncodeRange), [Encoded substringWithRange:EncodeRange]);
		}
	}
	NSData *Redata = [Encoded decodeBase64WithNewlines:NO];
	NSLog(@"Data  in: %d", [Data length]);
	NSLog(@"Data out: %d", [Redata length]);

//	for (i = 0; i < [Data length] + 1; i += 100) {
		DataRange = NSMakeRange([Data length] -1, 1);
		NSRange RedataRange = NSMakeRange([Redata length] -1, 1);
//		DataRange = NSIntersectionRange(DataRange, NSMakeRange(0, [Data length]));
//		NSRange RedataRange = NSMakeRange(i, 100);
//		RedataRange = NSIntersectionRange(RedataRange, NSMakeRange(0, [Redata length]));
//		if (![[Data subdataWithRange:DataRange] isEqualToData:[Redata subdataWithRange:RedataRange]]) {
			NSLog(@"Data  in [%@]: %@", NSStringFromRange(DataRange), [Data subdataWithRange:DataRange]);
			NSLog(@"Data out[%@]: %@", NSStringFromRange(RedataRange), [Redata subdataWithRange:RedataRange]);
//		}
//	}

	STAssertTrue([Data isEqualToData: Redata], @"Data not equal");
	STAssertTrue([Text isEqualToString: Encoded], @"Text not equal");
}
- (void) _testBase64PNG {
	NSString *Text = @"[encoded data, 191913 characters]";
	NSLog(@"Text: %d . %d", [Text length], [Text length] % 4);
	NSData *Data = [NSData dataWithContentsOfFile:@"/Applications/BitFonter 3/Samples/Construct72.phf-img.png.png"];
	NSLog(@"Data: %d . %d", [Data length], [Data length] % 3);
	NSRange DataRange = NSMakeRange([Data length] -20, 20);
	NSLog(@"readin Data  in [%@]: %@", NSStringFromRange(DataRange), [Data subdataWithRange:DataRange]);

	NSString *Encoded = [Data encodeBase64WithNewlines:NO];
	NSLog(@" in: %d", [Text length]);
	NSLog(@"out: %d", [Encoded length]);
	int i;
	for (i = 0; i < [Text length]; i += 100) {
		NSRange TextRange = NSMakeRange(i, 100);
		if (TextRange.location + TextRange.length > [Text length]) {
			TextRange.length = [Text length] - i;
		}
		NSRange EncodeRange = NSMakeRange(i, 100);
		if (EncodeRange.location + EncodeRange.length > [Encoded length]) {
			EncodeRange.length = [Encoded length] - i;
		}

		if (![[Text substringWithRange:TextRange] isEqualToString:[Encoded substringWithRange:EncodeRange]]) {
			NSLog(@"Text  in [%@]: %@", NSStringFromRange(TextRange), [Text substringWithRange:TextRange]);
			NSLog(@"Text out[%@]: %@", NSStringFromRange(EncodeRange), [Encoded substringWithRange:EncodeRange]);
		}
	}
	STAssertTrue([Text isEqualToString: Encoded], @"Text not equal");
}
@end
