Skip to content

Instantly share code, notes, and snippets.

@keisukeYamagishi
Created July 1, 2017 05:20
Show Gist options
  • Save keisukeYamagishi/331ae9f09d4ea5314bdc12ea3125f2b8 to your computer and use it in GitHub Desktop.
Save keisukeYamagishi/331ae9f09d4ea5314bdc12ea3125f2b8 to your computer and use it in GitHub Desktop.
//
// main.m
// Random
//
// Created by shichimi on 2017/07/01.
// Copyright © 2017年 shichimitoucarashi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Random.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[Random new]TEST];
}
return 0;
}
//
// Random.h
// Random
//
// Created by shichimi on 2017/07/01.
// Copyright © 2017年 shichimitoucarashi. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Random : NSObject
-(void)TEST;
-(void) TEST_RANDOM;
@end
//
// Random.m
// Random
//
// Created by shichimi on 2017/07/01.
// Copyright © 2017年 shichimitoucarashi. All rights reserved.
//
#import "Random.h"
#define TEST_COUNT_NUMBER 3
@implementation Random
-(NSArray *)randomArray:(NSArray *)array{
NSMutableArray* ary = array.mutableCopy;
for(NSInteger n = 0; n < array.count;n++){
NSInteger random = arc4random() % array.count;
[ary exchangeObjectAtIndex:n withObjectAtIndex:random];
}
return ary.mutableCopy;
}
-(void)TESTCODE:(NSArray*)ary
TESTVal:(void (^)(NSInteger i,NSInteger n,NSInteger same))handler{
NSInteger count = 0;
for(NSInteger i=0;i<ary.count;i++){
count = 0;
for(NSInteger n = 0;n<ary.count;n++){
handler(i,n,count);
}
}
}
-(void)TEST{
__block NSInteger num = 0;
__block BOOL isBUSTED = NO;
for(;num<TEST_COUNT_NUMBER;){
__block NSArray* random = [self randomArray:[self insertNumber:3]];
[self TESTCODE:random TESTVal:^(NSInteger i, NSInteger n,NSInteger same) {
if([random[i]integerValue] == [random[n]integerValue]){
same++;
if(same==2){
isBUSTED = YES;
}
}
if(isBUSTED)
printf("\n BUSTED Failuer \n");
if(i>=(random.count-1) && n>=(random.count-1)){
num++;
printf("COMPLETION %ld TIME TEST\n",num);
}
}];
}
printf("\nCompletion\n\n");
}
/*********************************************
*
* TEST CODE
*
*
*/
-(BOOL) TEST_RANDOM:(NSArray*)ary {
BOOL isUnique = NO;
for(NSInteger i=0;i<ary.count;i++){
int count = 0;
for(NSInteger n = 0;n<ary.count;n++){
if([ary[n]integerValue]==[ary[i]integerValue]){
count++;
if(count==2){
isUnique = YES;
}
}
}
}
return isUnique;
}
-(void) TEST_RANDOM {
for ( NSInteger num = 0; num < TEST_COUNT_NUMBER; num++ ){
NSArray* numbers = [self insertNumber:30];
NSArray* randoms = [self randomArray:numbers];
if( [self TEST_RANDOM:randoms] == YES ){
NSLog(@"Failuer Not unique and Random");
}
}
}
-(NSMutableArray*)insertNumber:(NSInteger)number{
NSMutableArray* array = [NSMutableArray array];
for(NSInteger i = 0; i<number;i++){
[array addObject:[NSNumber numberWithInteger:i]];
}
return array;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment