Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (123 sloc) 4.624 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/*
* Author: Landon Fuller <landonf@plausible.coop>
*
* Copyright 2010-2011 Plausible Labs Cooperative, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#import "GTMSenTestCase.h"
#import "blockimp.h"

@interface BlockIMPTests : SenTestCase @end

/**
* BlockIMP Tests
*/
@implementation BlockIMPTests

/**
* Test basic IMP allocation and execution.
*/
- (void) testAllocateIMP {
    /* A test block */
    __block BOOL didRun = NO;
    void (^Block)(id self) = ^(id blockself) {
        didRun = YES;

        STAssertEqualObjects(blockself, self, @"Incorrect 'self' pointer");
    };

    /* Create the IMP */
    IMP imp = pl_imp_implementationWithBlock(Block);
    STAssertTrue(imp != NULL, @"Returned NULL IMP");

    /* Verify the IMP is valid. */
    void (*ptr)(id self, SEL _cmd) = (void *) imp;
    ptr(self, @selector(testAllocateIMP));
    STAssertTrue(didRun, @"Block was not run");

    /* Clean up */
    pl_imp_removeBlock(imp);
}

/**
* Test basic stret IMP allocation and execution.
*/
- (void) testAllocateStretIMP {
    /* A test block */
    __block BOOL didRun = NO;
    NSRange (^Block)(id self) = ^NSRange (id blockself) {
        didRun = YES;

        STAssertEqualObjects(blockself, self, @"Incorrect 'self' pointer");
        return NSMakeRange(42, 1);
    };
    
    /* Create the IMP */
    IMP imp = pl_imp_implementationWithBlock(Block);
    STAssertTrue(imp != NULL, @"Returned NULL IMP");
    
    /* Verify the IMP is valid. */
    NSRange (*ptr)(id self, SEL _cmd) = (void *) imp;
    NSRange result = ptr(self, @selector(testAllocateStretIMP));

    STAssertTrue(didRun, @"Block was not run");
    STAssertEquals(result.location, (NSUInteger)42, @"Incorrect location");
    STAssertEquals(result.length, (NSUInteger)1, @"Incorrect length");
    
    /* Clean up */
    pl_imp_removeBlock(imp);
}

/**
* Test fetching of the Block ptr from the IMP pointer.
*/
- (void) testGetBlock {
    /* A test block */
    void (^Block)(id self) = [[^(id blockself) {
        STAssertEqualObjects(blockself, self, @"Incorrect 'self' pointer");
    } copy] autorelease];
    
    /* Create the IMP */
    IMP imp = pl_imp_implementationWithBlock(Block);
    STAssertTrue(imp != NULL, @"Returned NULL IMP");
    
    /* Try to fetch the underlying block */
    void *b = pl_imp_getBlock(imp);
    STAssertEquals(b, (void *) Block, @"Did not fetch block");
    
    /* Clean up */
    pl_imp_removeBlock(imp);
}

/**
* Exercise block allocation
*/
- (void) testAllocationExcercise {
    /* A test block */
    __block int callCount = 0;
    void (^Block)(id self) = [[^(id blockself) {
        callCount++;
        STAssertEqualObjects(blockself, self, @"Incorrect 'self' pointer");
    } copy] autorelease];

    /* Use a count larger than what a single page (on any architecture) can likely hold */
    int count=PAGE_SIZE*2;

    /* Generate the IMPs */
    IMP *imps = malloc(sizeof(IMP) * count);
    for (int i = 0; i < count; i++) {
        imps[i] = pl_imp_implementationWithBlock(Block);
    }

    /* Call the IMPs */
    for (int i = 0; i < count; i++) {
        void (*ptr)(id self, SEL _cmd) = (void *) imps[i];
        ptr(self, @selector(testAllocationExcercise));
    }

    /* Clean up the IMPs. We do this in reverse to exercise table reordering. */
    for (int i = count-1; i+1 >= 1; i--) {
        pl_imp_removeBlock(imps[i]);
    }
    free(imps);

    /* Verify the result */
    STAssertEquals(callCount, count, @"Call count does not match expected count; not all IMPs were called");
}

@end
Something went wrong with that request. Please try again.