2 // Xd3iOSViewController.m
5 // Created by Joshua MacDonald on 6/16/12.
6 // Copyright (c) 2011, 2012 Joshua MacDonald. All rights reserved.
9 #import "Xd3iOSViewController.h"
11 #include "dispatch/queue.h"
12 #include "Foundation/NSBundle.h"
14 extern void (*xprintf_message_func)(const char* msg);
15 void print_to_view(const char* buf);
16 int xd3_main_cmdline(int argc, char **argv);
17 void do_localfile_test(void);
18 int compare_files(const char* file1, const char* file2);
19 Xd3iOSViewController *static_ptr;
21 @implementation Xd3iOSViewController
22 @synthesize theSeed = _theSeed;
23 @synthesize theView = _theView;
24 @synthesize theOutput = _theOutput;
25 @synthesize inTest = _inTest;
27 - (void)didReceiveMemoryWarning
29 [super didReceiveMemoryWarning];
32 #pragma mark - View lifecycle
41 [self setTheSeed:nil];
42 [self setTheView:nil];
43 [self setTheView:nil];
44 [super viewDidUnload];
47 - (void)viewWillAppear:(BOOL)animated
49 [super viewWillAppear:animated];
52 - (void)viewDidAppear:(BOOL)animated
54 [super viewDidAppear:animated];
57 - (void)viewWillDisappear:(BOOL)animated
59 [super viewWillDisappear:animated];
62 - (void)viewDidDisappear:(BOOL)animated
64 [super viewDidDisappear:animated];
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
69 switch (interfaceOrientation) {
70 case UIInterfaceOrientationPortrait:
71 case UIInterfaceOrientationPortraitUpsideDown:
78 - (BOOL)textFieldShouldReturn:(UITextField*)theTextField {
79 if (theTextField == self.theSeed) {
80 [theTextField resignFirstResponder];
84 - (IBAction)startTest:(id)sender {
89 NSString *seedString = self.theSeed.text;
90 if ([seedString length] == 0) {
91 seedString = @"RFC3284";
94 xprintf_message_func = &print_to_view;
95 self.theOutput = [[NSMutableString alloc] initWithFormat:@"Starting test (seed=%@)\n", seedString];
96 self.theView.text = self.theOutput;
97 dispatch_queue_t mq = dispatch_get_main_queue();
98 dispatch_queue_t dq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
101 char *argv[] = { "xdelta3", "test", NULL };
102 xd3_main_cmdline(2, argv);
103 print_to_view("Finished unittest: success");
104 dispatch_async(mq, ^{
110 void printns_to_view(NSString* ns);
111 void printns_to_view(NSString* ns) {
112 dispatch_queue_t mq = dispatch_get_main_queue();
113 dispatch_async(mq, ^{
114 if ([static_ptr.theOutput length] < 25000) {
115 [static_ptr.theOutput appendString:ns];
117 static_ptr.theOutput = [[NSMutableString alloc] initWithString:ns];
119 static_ptr.theView.text = static_ptr.theOutput;
120 CGSize size = static_ptr.theView.contentSize;
121 [static_ptr.theView scrollRectToVisible:CGRectMake(0, size.height - 1, 1, 1) animated:NO];
125 void print_to_view(const char* buf) {
126 NSString *ns = [NSString stringWithCString:buf encoding:NSASCIIStringEncoding];
130 void do_localfile_test(void) {
132 bundle = [NSBundle mainBundle];
133 NSString *localfile1 = [bundle pathForResource:@"file_v1" ofType:@"bin"];
134 NSString *localfile2 = [bundle pathForResource:@"file_v2" ofType:@"bin"];
135 NSString *localfiled = [bundle pathForResource:@"file_v1_to_v2" ofType:@"bin"];
136 printns_to_view([localfile1 stringByAppendingString:@"\n"]);
137 printns_to_view([localfile2 stringByAppendingString:@"\n"]);
138 printns_to_view([localfiled stringByAppendingString:@"\n"]);
139 NSString *tmpdir = NSTemporaryDirectory();
140 NSString *tmpfile = [tmpdir stringByAppendingPathComponent:@"delta.tmp"];
141 printns_to_view([tmpfile stringByAppendingString:@"\n"]);
143 "xdelta3", "-dfvv", "-s",
144 (char*)[localfile1 UTF8String],
145 (char*)[localfiled UTF8String],
146 (char*)[tmpfile UTF8String] };
147 xd3_main_cmdline(6, argv);
149 NSFileManager *filemgr;
151 filemgr = [NSFileManager defaultManager];
153 if ([filemgr contentsEqualAtPath: localfile2 andPath: tmpfile] == YES) {
154 printns_to_view(@"File contents match\n");
156 NSError *err1 = NULL;
157 NSDictionary *d1 = [filemgr attributesOfItemAtPath: tmpfile error: &err1];
159 printns_to_view([@"File localfile2 could not stat %s\n" stringByAppendingString: tmpfile]);
161 printns_to_view([@"File contents do not match!!!! tmpfile size=" stringByAppendingString:
162 [[NSMutableString alloc] initWithFormat:@"%llu\n", [d1 fileSize]]]);
164 compare_files([localfile2 UTF8String], [tmpfile UTF8String]);
166 print_to_view("Finished localfile test.\n");