<commit_before>// Copyright 2020 Penguin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
import PenguinStructures

final class DoubleEndedBufferTests: XCTestCase {

	func testSimple() {
		var b = DoubleEndedBuffer<Int>(capacity: 10, with: .beginning)
		XCTAssert(b.isEmpty)
		XCTAssert(b.canPushBack)
		XCTAssertFalse(b.canPushFront)
		for i in 0..<10 {
			XCTAssert(b.canPushBack)
			XCTAssertEqual(i, b.count)
			b.pushBack(i)
			XCTAssertFalse(b.isEmpty)
		}
		// XCTAssertFalse(b.canPushBack) // Does not hold true on Linux, which allocates a larger-than-required buffer.
		for i in 0..<10 {
			XCTAssertFalse(b.isEmpty)
			XCTAssertEqual(i, b.popFront())
		}
		XCTAssert(b.isEmpty)
		// XCTAssertFalse(b.canPushBack) // Does not hold true on Linux, which allocates a larger-than-required buffer.
		for i in 0..<10 {
			XCTAssert(b.canPushFront)
			b.pushFront(i)
		}
		XCTAssertFalse(b.canPushFront)
	}

	func testInitializations() {
		let beginning = DoubleEndedBuffer<Int>(capacity: 10, with: .beginning)
		XCTAssert(beginning.canPushBack)
		XCTAssertFalse(beginning.canPushFront)

		let middle = DoubleEndedBuffer<Int>(capacity: 10, with: .middle)
		XCTAssert(middle.canPushFront)
		XCTAssert(middle.canPushBack)

		let end = DoubleEndedBuffer<Int>(capacity: 10, with: .end)
		XCTAssertFalse(end.canPushBack)
		XCTAssert(end.canPushFront)
	}

	func testCollection() {
		var b = DoubleEndedBuffer<Int>(capacity: 10, with: .middle)
		b.pushFront(2)
		b.pushFront(1)
		b.pushFront(0)

		XCTAssertEqual([0, 1, 2], Array(b))

		b.pushBack(3)
		b.pushBack(4)
		XCTAssertEqual([0, 1, 2, 3, 4], Array(b))

		XCTAssertEqual(3, b[5])
		b[5] = 10
		XCTAssertEqual([0, 1, 2, 10, 4], Array(b))		
	}

	func testMemoryLeaks() {
		XCTAssert(MemoryChecker.allDeleted)

		do {
			var b = DoubleEndedBuffer<MemoryChecker>(capacity: 10, with: .end)
			b.pushFront(MemoryChecker())
			XCTAssertFalse(MemoryChecker.allDeleted)
		}
		XCTAssert(MemoryChecker.allDeleted)

		do {
			let startCount = MemoryChecker.idCounter
			var b = DoubleEndedBuffer<MemoryChecker>(capacity: 10, with: .beginning)
			b.pushBack(MemoryChecker())
			b[0] = MemoryChecker()
			XCTAssertEqual(2, MemoryChecker.idCounter - startCount)
			XCTAssertEqual(1, MemoryChecker.idCounter - MemoryChecker.deletedCount)<commit_msg>Add additional assertion to ensure the Swift compiler isn't too clever.<commit_after>// Copyright 2020 Penguin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
import PenguinStructures

final class DoubleEndedBufferTests: XCTestCase {

	func testSimple() {
		var b = DoubleEndedBuffer<Int>(capacity: 10, with: .beginning)
		XCTAssert(b.isEmpty)
		XCTAssert(b.canPushBack)
		XCTAssertFalse(b.canPushFront)
		for i in 0..<10 {
			XCTAssert(b.canPushBack)
			XCTAssertEqual(i, b.count)
			b.pushBack(i)
			XCTAssertFalse(b.isEmpty)
		}
		// XCTAssertFalse(b.canPushBack) // Does not hold true on Linux, which allocates a larger-than-required buffer.
		for i in 0..<10 {
			XCTAssertFalse(b.isEmpty)
			XCTAssertEqual(i, b.popFront())
		}
		XCTAssert(b.isEmpty)
		// XCTAssertFalse(b.canPushBack) // Does not hold true on Linux, which allocates a larger-than-required buffer.
		for i in 0..<10 {
			XCTAssert(b.canPushFront)
			b.pushFront(i)
		}
		XCTAssertFalse(b.canPushFront)
	}

	func testInitializations() {
		let beginning = DoubleEndedBuffer<Int>(capacity: 10, with: .beginning)
		XCTAssert(beginning.canPushBack)
		XCTAssertFalse(beginning.canPushFront)

		let middle = DoubleEndedBuffer<Int>(capacity: 10, with: .middle)
		XCTAssert(middle.canPushFront)
		XCTAssert(middle.canPushBack)

		let end = DoubleEndedBuffer<Int>(capacity: 10, with: .end)
		XCTAssertFalse(end.canPushBack)
		XCTAssert(end.canPushFront)
	}

	func testCollection() {
		var b = DoubleEndedBuffer<Int>(capacity: 10, with: .middle)
		b.pushFront(2)
		b.pushFront(1)
		b.pushFront(0)

		XCTAssertEqual([0, 1, 2], Array(b))

		b.pushBack(3)
		b.pushBack(4)
		XCTAssertEqual([0, 1, 2, 3, 4], Array(b))

		XCTAssertEqual(3, b[5])
		b[5] = 10
		XCTAssertEqual([0, 1, 2, 10, 4], Array(b))		
	}

	func testMemoryLeaks() {
		XCTAssert(MemoryChecker.allDeleted)

		do {
			var b = DoubleEndedBuffer<MemoryChecker>(capacity: 10, with: .end)
			b.pushFront(MemoryChecker())
			XCTAssertFalse(MemoryChecker.allDeleted)
			XCTAssert(b.canPushFront)  // Necessary assertion; optimized builds make the above line
			// fail, due to optimizing away the operations, and thus deallocate `b` before we can
			// assert that `MemoryChecker.allDeleted` is false.
		}
		XCTAssert(MemoryChecker.allDeleted)

		do {
			let startCount = MemoryChecker.idCounter
			var b = DoubleEndedBuffer<MemoryChecker>(capacity: 10, with: .beginning)
			b.pushBack(MemoryChecker())
			b[0] = MemoryChecker()
			XCTAssertEqual(2, MemoryChecker.idCounter - startCount)
			XCTAssertEqual(1, MemoryChecker.idCounter - MemoryChecker.deletedCount)
{'fraction_non_alphanumeric': 0.1129657730568201, 'fraction_numerical': 0.019558253245658404, 'mean_word_length': 4.167247386759582, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6436881', 'n_tokens_mistral': 2234, 'n_tokens_neox': 1998, 'n_words': 519}