<commit_before>//
//  yas_objc_container_tests.m
//  Copyright (c) 2015 <NAME>.
//

#import "yas_audio_test_utils.h"

static int _objc_object_count = 0;

@interface YASObjCContainerTest : NSObject

@end

@implementation YASObjCContainerTest

- (instancetype)init
{
    self = [super init];
    if (self) {
        _objc_object_count++;
    }
    return self;
}

- (void)dealloc
{
    _objc_object_count--;
    [super dealloc];
}

@end

@interface yas_objc_container_tests : XCTestCase

@end

@implementation yas_objc_container_tests

- (void)setUp
{
    [super setUp];

    _objc_object_count = 0;
}

- (void)tearDown
{
    [super tearDown];
}

- (void)test_retain_objc_object_on_strong_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    if (auto objc_strong_container = yas::objc::container<>(objc_object)) {
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 2);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_unretain_objc_object_on_weak_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    if (auto objc_weak_container = yas::objc::container<yas::objc::weak>(objc_object)) {
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_set_objc_object_on_weak_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    if (auto objc_weak_container = yas::objc::container<yas::objc::weak>(nil)) {
        XCTAssertNil(objc_weak_container.retained_object());
        XCTAssertNil(objc_weak_container.autoreleased_object());
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);

        objc_weak_container.set_object(objc_object);

        id retained_object = objc_weak_container.retained_object();

        XCTAssertNotNil(retained_object);
        XCTAssertEqual([objc_object retainCount], 2);

        [objc_object release];
        retained_object = nil;

        XCTAssertEqual([objc_object retainCount], 1);

        @autoreleasepool
        {
            id autoreleased_object = objc_weak_container.autoreleased_object();

            XCTAssertNotNil(autoreleased_object);
            XCTAssertEqual([objc_object retainCount], 2);
        }

        XCTAssertEqual([objc_object retainCount], 1);

        objc_weak_container.set_object(nil);

        XCTAssertNil(objc_weak_container.retained_object());
        XCTAssertNil(objc_weak_container.autoreleased_object());
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_set_objec_object_on_strong_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    if (auto objc_strong_container = yas::objc::container<>(nil)) {
        XCTAssertNil(objc_strong_container.retained_object());
        XCTAssertNil(objc_strong_container.autoreleased_object());
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);

        objc_strong_container.set_object(objc_object);

        XCTAssertEqual([objc_object retainCount], 2);

        id retained_object = objc_strong_container.retained_object();

        XCTAssertNotNil(retained_object);
        XCTAssertEqual([objc_object retainCount], 3);

        [objc_object release];
        retained_object = nil;

        XCTAssertEqual([objc_object retainCount], 2);

        @autoreleasepool
        {
            id autoreleased_object = objc_strong_container.autoreleased_object();

            XCTAssertNotNil(autoreleased_object);
            XCTAssertEqual([objc_object retainCount], 3);
        }

        XCTAssertEqual([objc_object retainCount], 2);

        objc_strong_container.set_object(nil);

        XCTAssertNil(objc_strong_container.retained_object());
        XCTAssertNil(objc_strong_container.autoreleased_object());
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

#pragma mark - copy

- (void)test_copy_strong_to_strong
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual([objc_object retainCount], 1);

    {
        yas::objc::container<> objc_strong_container1(objc_object);

        XCTAssertEqual([objc_object retainCount], 2);

        yas::objc::container<> objc_strong_container2(objc_object);

        XCTAssertEqual([objc_object retainCount], 3);

        objc_strong_container1 = objc_strong_container2;

        XCTAssertEqual([objc_object retainCount], 3);
    }

    XCTAssertEqual([objc_object retainCount], 1);
<commit_msg>fix objc_container_tests<commit_after>//
//  yas_objc_container_tests.m
//  Copyright (c) 2015 <NAME>.
//

#import "yas_audio_test_utils.h"

static int _objc_object_count = 0;

@interface YASObjCContainerTest : NSObject

@end

@implementation YASObjCContainerTest

- (instancetype)init
{
    self = [super init];
    if (self) {
        _objc_object_count++;
    }
    return self;
}

- (void)dealloc
{
    _objc_object_count--;
    [super dealloc];
}

@end

@interface yas_objc_container_tests : XCTestCase

@end

@implementation yas_objc_container_tests

- (void)setUp
{
    [super setUp];

    _objc_object_count = 0;
}

- (void)tearDown
{
    [super tearDown];
}

- (void)test_retain_objc_object_on_strong_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    if (auto objc_strong_container = yas::objc::container<>(objc_object)) {
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 2);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_unretain_objc_object_on_weak_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    if (auto objc_weak_container = yas::objc::container<yas::objc::weak>(objc_object)) {
        XCTAssertEqual(_objc_object_count, 1);
        XCTAssertEqual([objc_object retainCount], 1);
    }

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_set_objc_object_on_weak_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    auto objc_weak_container = yas::objc::container<yas::objc::weak>(nil);
    XCTAssertNil(objc_weak_container.retained_object());
    XCTAssertNil(objc_weak_container.autoreleased_object());
    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    objc_weak_container.set_object(objc_object);

    id retained_object = objc_weak_container.retained_object();

    XCTAssertNotNil(retained_object);
    XCTAssertEqual([objc_object retainCount], 2);

    [objc_object release];
    retained_object = nil;

    XCTAssertEqual([objc_object retainCount], 1);

    @autoreleasepool
    {
        id autoreleased_object = objc_weak_container.autoreleased_object();

        XCTAssertNotNil(autoreleased_object);
        XCTAssertEqual([objc_object retainCount], 2);
    }

    XCTAssertEqual([objc_object retainCount], 1);

    objc_weak_container.set_object(nil);

    XCTAssertNil(objc_weak_container.retained_object());
    XCTAssertNil(objc_weak_container.autoreleased_object());
    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

- (void)test_set_objec_object_on_strong_container
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    auto objc_strong_container = yas::objc::container<>(nil);
    XCTAssertNil(objc_strong_container.retained_object());
    XCTAssertNil(objc_strong_container.autoreleased_object());
    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    objc_strong_container.set_object(objc_object);

    XCTAssertEqual([objc_object retainCount], 2);

    id retained_object = objc_strong_container.retained_object();

    XCTAssertNotNil(retained_object);
    XCTAssertEqual([objc_object retainCount], 3);

    [objc_object release];
    retained_object = nil;

    XCTAssertEqual([objc_object retainCount], 2);

    @autoreleasepool
    {
        id autoreleased_object = objc_strong_container.autoreleased_object();

        XCTAssertNotNil(autoreleased_object);
        XCTAssertEqual([objc_object retainCount], 3);
    }

    XCTAssertEqual([objc_object retainCount], 2);

    objc_strong_container.set_object(nil);

    XCTAssertNil(objc_strong_container.retained_object());
    XCTAssertNil(objc_strong_container.autoreleased_object());
    XCTAssertEqual(_objc_object_count, 1);
    XCTAssertEqual([objc_object retainCount], 1);

    [objc_object release];
    objc_object = nil;

    XCTAssertEqual(_objc_object_count, 0);
}

#pragma mark - copy

- (void)test_copy_strong_to_strong
{
    YASObjCContainerTest *objc_object = [[YASObjCContainerTest alloc] init];

    XCTAssertEqual([objc_object retainCount], 1);

    {
        yas::objc::container<> objc_strong_container1(objc_object);

        XCTAssertEqual([objc_object retainCount], 2);

        yas::objc::container<> objc_strong_container2(objc_object);

        XCTAssertEqual([objc_object retainCount], 3);

        objc_strong_container1 = objc_strong_container2;

        XCTAssertEqual([objc_object retainCount], 3);
    }

    XCTAssertEqual([objc_object retainCount], 1);

{'fraction_non_alphanumeric': 0.10318310133359292, 'fraction_numerical': 0.00856614426165677, 'mean_word_length': 4.263319672131147, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3519083', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3516, 'n_words': 489}