tizen 2.4 release
[external/xdelta3.git] / examples / iOS / xdelta3-ios-test / xdelta3-ios-test / Xd3iOSViewController.m
1 //
2 //  Xd3iOSViewController.m
3 //  xdelta3-ios-test
4 //
5 //  Created by Joshua MacDonald on 6/16/12.
6 //  Copyright (c) 2011, 2012 Joshua MacDonald. All rights reserved.
7 //
8
9 #import "Xd3iOSViewController.h"
10 #include "xdelta3.h"
11 #include "dispatch/queue.h"
12 #include "Foundation/NSBundle.h"
13
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;
20
21 @implementation Xd3iOSViewController
22 @synthesize theSeed = _theSeed;
23 @synthesize theView = _theView;
24 @synthesize theOutput = _theOutput;
25 @synthesize inTest = _inTest;
26
27 - (void)didReceiveMemoryWarning
28 {
29     [super didReceiveMemoryWarning];
30 }
31
32 #pragma mark - View lifecycle
33
34 - (void)viewDidLoad
35 {
36     [super viewDidLoad];
37 }
38
39 - (void)viewDidUnload
40 {
41     [self setTheSeed:nil];
42     [self setTheView:nil];
43     [self setTheView:nil];
44     [super viewDidUnload];
45 }
46
47 - (void)viewWillAppear:(BOOL)animated
48 {
49     [super viewWillAppear:animated];
50 }
51
52 - (void)viewDidAppear:(BOOL)animated
53 {
54     [super viewDidAppear:animated];
55 }
56
57 - (void)viewWillDisappear:(BOOL)animated
58 {
59         [super viewWillDisappear:animated];
60 }
61
62 - (void)viewDidDisappear:(BOOL)animated
63 {
64         [super viewDidDisappear:animated];
65 }
66
67 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
68 {
69     switch (interfaceOrientation) {
70         case UIInterfaceOrientationPortrait:
71         case UIInterfaceOrientationPortraitUpsideDown:
72             return YES;
73         default:
74             break;
75     }
76     return NO;
77 }
78 - (BOOL)textFieldShouldReturn:(UITextField*)theTextField {
79     if (theTextField == self.theSeed) {
80         [theTextField resignFirstResponder];
81     }
82     return YES;
83 }
84 - (IBAction)startTest:(id)sender {
85     if (self.inTest) {
86         return;
87     }
88     self.inTest = YES;
89     NSString *seedString = self.theSeed.text;
90     if ([seedString length] == 0) {
91         seedString = @"RFC3284";
92     }
93     static_ptr = self;
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);
99     dispatch_async(dq, ^{
100         do_localfile_test();
101         char *argv[] = { "xdelta3", "test", NULL };
102         xd3_main_cmdline(2, argv);
103         print_to_view("Finished unittest: success");
104         dispatch_async(mq, ^{
105             self.inTest = NO;
106         });
107     });
108 }
109
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];
116         } else {
117             static_ptr.theOutput = [[NSMutableString alloc] initWithString:ns];
118         }
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];
122     });
123 }
124
125 void print_to_view(const char* buf) {
126     NSString *ns = [NSString stringWithCString:buf encoding:NSASCIIStringEncoding];
127     printns_to_view(ns);
128 }
129
130 void do_localfile_test(void) {
131     NSBundle *bundle;
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"]);
142     char *argv[] = { 
143         "xdelta3", "-dfvv", "-s", 
144         (char*)[localfile1 UTF8String],
145         (char*)[localfiled UTF8String],
146         (char*)[tmpfile UTF8String] };
147     xd3_main_cmdline(6, argv);
148
149     NSFileManager *filemgr;
150
151     filemgr = [NSFileManager defaultManager];
152     
153     if ([filemgr contentsEqualAtPath: localfile2 andPath: tmpfile] == YES) {
154         printns_to_view(@"File contents match\n");
155     } else {
156         NSError *err1 = NULL;
157         NSDictionary *d1 = [filemgr attributesOfItemAtPath: tmpfile error: &err1];
158         if (err1 != NULL) {
159             printns_to_view([@"File localfile2 could not stat %s\n" stringByAppendingString: tmpfile]);
160         } else {
161             printns_to_view([@"File contents do not match!!!! tmpfile size=" stringByAppendingString:
162                              [[NSMutableString alloc] initWithFormat:@"%llu\n", [d1 fileSize]]]);
163         }
164         compare_files([localfile2 UTF8String], [tmpfile UTF8String]);
165     }
166     print_to_view("Finished localfile test.\n");
167 }
168
169 @end