Skip to content

Commit 7a0417e

Browse files
committed
Added some tests. There are a failing test in the original code.
1 parent 9ac2d76 commit 7a0417e

File tree

5 files changed

+457
-0
lines changed

5 files changed

+457
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Foundation
2+
import XCTest
3+
4+
class BoyerMooreTest: XCTestCase {
5+
override func setUp() {
6+
super.setUp()
7+
}
8+
9+
func assert(pattern: String, doesNotExistsIn string: String) {
10+
let index = string.indexOf(pattern: pattern)
11+
XCTAssertNil(index)
12+
}
13+
14+
func assert(pattern: String, existsIn string: String) {
15+
let index = string.indexOf(pattern: pattern)
16+
XCTAssertNotNil(index)
17+
18+
let startIndex = index!
19+
let endIndex = string.index(index!, offsetBy: pattern.characters.count)
20+
let match = string.substring(with: startIndex..<endIndex)
21+
XCTAssertEqual(match, pattern)
22+
}
23+
24+
func testSearchTheSameString() {
25+
let string = "ABCDEF"
26+
let pattern = "ABCDEF"
27+
assert(pattern: pattern, existsIn: string)
28+
}
29+
30+
func testSearchAPreffix() {
31+
let string = "ABCDEFGHIJK"
32+
let pattern = "ABCDEF"
33+
assert(pattern: pattern, existsIn: string)
34+
}
35+
36+
func testSearchASuffix() {
37+
let string = "ABCDEFGHIJK"
38+
let pattern = "HIJK"
39+
assert(pattern: pattern, existsIn: string)
40+
}
41+
42+
func testSearchAStringFromTheMiddle() {
43+
let string = "ABCDEFGHIJK"
44+
let pattern = "EFG"
45+
assert(pattern: pattern, existsIn: string)
46+
}
47+
48+
49+
func testSearchInvalidPattern() {
50+
let string = "ABCDEFGHIJK"
51+
let pattern = "AEF"
52+
assert(pattern: pattern, doesNotExistsIn: string)
53+
}
54+
55+
func testSearchPatternWithDuplicatedCharacter() {
56+
let string = "Goal: Write a string search algorithm in pure Swift without importing Foundation or using NSString's rangeOfString() method."
57+
let pattern = "NSS"
58+
assert(pattern: pattern, existsIn: string)
59+
}
60+
61+
func testSearchInvalidPatternWithDuplicatedCharacter() {
62+
let string = "Goal: Write a string search algorithm in pure Swift without importing Foundation or using NSString's rangeOfString() method."
63+
let pattern = "nss"
64+
assert(pattern: pattern, doesNotExistsIn: string)
65+
}
66+
67+
}

Boyer-Moore/Tests/Info.plist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
</dict>
24+
</plist>
Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
// !$*UTF8*$!
2+
{
3+
archiveVersion = 1;
4+
classes = {
5+
};
6+
objectVersion = 46;
7+
objects = {
8+
9+
/* Begin PBXBuildFile section */
10+
7B80C3CE1C77A256003CECC7 /* BoyerMooreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B80C3CD1C77A256003CECC7 /* BoyerMooreTests.swift */; };
11+
9AED567C1E0ED6DE00958DCC /* BoyerMoore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9AED567B1E0ED6DE00958DCC /* BoyerMoore.swift */; };
12+
/* End PBXBuildFile section */
13+
14+
/* Begin PBXFileReference section */
15+
7B2BBC801C779D720067B71D /* Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
16+
7B2BBC941C779E7B0067B71D /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; };
17+
7B80C3CD1C77A256003CECC7 /* BoyerMooreTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoyerMooreTests.swift; sourceTree = SOURCE_ROOT; };
18+
9AED567B1E0ED6DE00958DCC /* BoyerMoore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BoyerMoore.swift; path = ../../BoyerMoore.swift; sourceTree = "<group>"; };
19+
/* End PBXFileReference section */
20+
21+
/* Begin PBXFrameworksBuildPhase section */
22+
7B2BBC7D1C779D720067B71D /* Frameworks */ = {
23+
isa = PBXFrameworksBuildPhase;
24+
buildActionMask = 2147483647;
25+
files = (
26+
);
27+
runOnlyForDeploymentPostprocessing = 0;
28+
};
29+
/* End PBXFrameworksBuildPhase section */
30+
31+
/* Begin PBXGroup section */
32+
7B2BBC681C779D710067B71D = {
33+
isa = PBXGroup;
34+
children = (
35+
7B2BBC831C779D720067B71D /* Tests */,
36+
7B2BBC721C779D710067B71D /* Products */,
37+
);
38+
sourceTree = "<group>";
39+
};
40+
7B2BBC721C779D710067B71D /* Products */ = {
41+
isa = PBXGroup;
42+
children = (
43+
7B2BBC801C779D720067B71D /* Tests.xctest */,
44+
);
45+
name = Products;
46+
sourceTree = "<group>";
47+
};
48+
7B2BBC831C779D720067B71D /* Tests */ = {
49+
isa = PBXGroup;
50+
children = (
51+
9AED567B1E0ED6DE00958DCC /* BoyerMoore.swift */,
52+
7B80C3CD1C77A256003CECC7 /* BoyerMooreTests.swift */,
53+
7B2BBC941C779E7B0067B71D /* Info.plist */,
54+
);
55+
name = Tests;
56+
path = TestsTests;
57+
sourceTree = "<group>";
58+
};
59+
/* End PBXGroup section */
60+
61+
/* Begin PBXNativeTarget section */
62+
7B2BBC7F1C779D720067B71D /* Tests */ = {
63+
isa = PBXNativeTarget;
64+
buildConfigurationList = 7B2BBC8C1C779D720067B71D /* Build configuration list for PBXNativeTarget "Tests" */;
65+
buildPhases = (
66+
7B2BBC7C1C779D720067B71D /* Sources */,
67+
7B2BBC7D1C779D720067B71D /* Frameworks */,
68+
7B2BBC7E1C779D720067B71D /* Resources */,
69+
);
70+
buildRules = (
71+
);
72+
dependencies = (
73+
);
74+
name = Tests;
75+
productName = TestsTests;
76+
productReference = 7B2BBC801C779D720067B71D /* Tests.xctest */;
77+
productType = "com.apple.product-type.bundle.unit-test";
78+
};
79+
/* End PBXNativeTarget section */
80+
81+
/* Begin PBXProject section */
82+
7B2BBC691C779D710067B71D /* Project object */ = {
83+
isa = PBXProject;
84+
attributes = {
85+
LastSwiftUpdateCheck = 0720;
86+
LastUpgradeCheck = 0800;
87+
ORGANIZATIONNAME = "Swift Algorithm Club";
88+
TargetAttributes = {
89+
7B2BBC7F1C779D720067B71D = {
90+
CreatedOnToolsVersion = 7.2;
91+
LastSwiftMigration = 0800;
92+
};
93+
};
94+
};
95+
buildConfigurationList = 7B2BBC6C1C779D710067B71D /* Build configuration list for PBXProject "Tests" */;
96+
compatibilityVersion = "Xcode 3.2";
97+
developmentRegion = English;
98+
hasScannedForEncodings = 0;
99+
knownRegions = (
100+
en,
101+
Base,
102+
);
103+
mainGroup = 7B2BBC681C779D710067B71D;
104+
productRefGroup = 7B2BBC721C779D710067B71D /* Products */;
105+
projectDirPath = "";
106+
projectRoot = "";
107+
targets = (
108+
7B2BBC7F1C779D720067B71D /* Tests */,
109+
);
110+
};
111+
/* End PBXProject section */
112+
113+
/* Begin PBXResourcesBuildPhase section */
114+
7B2BBC7E1C779D720067B71D /* Resources */ = {
115+
isa = PBXResourcesBuildPhase;
116+
buildActionMask = 2147483647;
117+
files = (
118+
);
119+
runOnlyForDeploymentPostprocessing = 0;
120+
};
121+
/* End PBXResourcesBuildPhase section */
122+
123+
/* Begin PBXSourcesBuildPhase section */
124+
7B2BBC7C1C779D720067B71D /* Sources */ = {
125+
isa = PBXSourcesBuildPhase;
126+
buildActionMask = 2147483647;
127+
files = (
128+
9AED567C1E0ED6DE00958DCC /* BoyerMoore.swift in Sources */,
129+
7B80C3CE1C77A256003CECC7 /* BoyerMooreTests.swift in Sources */,
130+
);
131+
runOnlyForDeploymentPostprocessing = 0;
132+
};
133+
/* End PBXSourcesBuildPhase section */
134+
135+
/* Begin XCBuildConfiguration section */
136+
7B2BBC871C779D720067B71D /* Debug */ = {
137+
isa = XCBuildConfiguration;
138+
buildSettings = {
139+
ALWAYS_SEARCH_USER_PATHS = NO;
140+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
141+
CLANG_CXX_LIBRARY = "libc++";
142+
CLANG_ENABLE_MODULES = YES;
143+
CLANG_ENABLE_OBJC_ARC = YES;
144+
CLANG_WARN_BOOL_CONVERSION = YES;
145+
CLANG_WARN_CONSTANT_CONVERSION = YES;
146+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
147+
CLANG_WARN_EMPTY_BODY = YES;
148+
CLANG_WARN_ENUM_CONVERSION = YES;
149+
CLANG_WARN_INFINITE_RECURSION = YES;
150+
CLANG_WARN_INT_CONVERSION = YES;
151+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
152+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
153+
CLANG_WARN_UNREACHABLE_CODE = YES;
154+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
155+
CODE_SIGN_IDENTITY = "-";
156+
COPY_PHASE_STRIP = NO;
157+
DEBUG_INFORMATION_FORMAT = dwarf;
158+
ENABLE_STRICT_OBJC_MSGSEND = YES;
159+
ENABLE_TESTABILITY = YES;
160+
GCC_C_LANGUAGE_STANDARD = gnu99;
161+
GCC_DYNAMIC_NO_PIC = NO;
162+
GCC_NO_COMMON_BLOCKS = YES;
163+
GCC_OPTIMIZATION_LEVEL = 0;
164+
GCC_PREPROCESSOR_DEFINITIONS = (
165+
"DEBUG=1",
166+
"$(inherited)",
167+
);
168+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
169+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
170+
GCC_WARN_UNDECLARED_SELECTOR = YES;
171+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
172+
GCC_WARN_UNUSED_FUNCTION = YES;
173+
GCC_WARN_UNUSED_VARIABLE = YES;
174+
MACOSX_DEPLOYMENT_TARGET = 10.11;
175+
MTL_ENABLE_DEBUG_INFO = YES;
176+
ONLY_ACTIVE_ARCH = YES;
177+
SDKROOT = macosx;
178+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
179+
};
180+
name = Debug;
181+
};
182+
7B2BBC881C779D720067B71D /* Release */ = {
183+
isa = XCBuildConfiguration;
184+
buildSettings = {
185+
ALWAYS_SEARCH_USER_PATHS = NO;
186+
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
187+
CLANG_CXX_LIBRARY = "libc++";
188+
CLANG_ENABLE_MODULES = YES;
189+
CLANG_ENABLE_OBJC_ARC = YES;
190+
CLANG_WARN_BOOL_CONVERSION = YES;
191+
CLANG_WARN_CONSTANT_CONVERSION = YES;
192+
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
193+
CLANG_WARN_EMPTY_BODY = YES;
194+
CLANG_WARN_ENUM_CONVERSION = YES;
195+
CLANG_WARN_INFINITE_RECURSION = YES;
196+
CLANG_WARN_INT_CONVERSION = YES;
197+
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
198+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
199+
CLANG_WARN_UNREACHABLE_CODE = YES;
200+
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
201+
CODE_SIGN_IDENTITY = "-";
202+
COPY_PHASE_STRIP = NO;
203+
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
204+
ENABLE_NS_ASSERTIONS = NO;
205+
ENABLE_STRICT_OBJC_MSGSEND = YES;
206+
GCC_C_LANGUAGE_STANDARD = gnu99;
207+
GCC_NO_COMMON_BLOCKS = YES;
208+
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
209+
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
210+
GCC_WARN_UNDECLARED_SELECTOR = YES;
211+
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
212+
GCC_WARN_UNUSED_FUNCTION = YES;
213+
GCC_WARN_UNUSED_VARIABLE = YES;
214+
MACOSX_DEPLOYMENT_TARGET = 10.11;
215+
MTL_ENABLE_DEBUG_INFO = NO;
216+
SDKROOT = macosx;
217+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
218+
};
219+
name = Release;
220+
};
221+
7B2BBC8D1C779D720067B71D /* Debug */ = {
222+
isa = XCBuildConfiguration;
223+
buildSettings = {
224+
COMBINE_HIDPI_IMAGES = YES;
225+
INFOPLIST_FILE = Info.plist;
226+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
227+
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
228+
PRODUCT_NAME = "$(TARGET_NAME)";
229+
SWIFT_VERSION = 3.0;
230+
};
231+
name = Debug;
232+
};
233+
7B2BBC8E1C779D720067B71D /* Release */ = {
234+
isa = XCBuildConfiguration;
235+
buildSettings = {
236+
COMBINE_HIDPI_IMAGES = YES;
237+
INFOPLIST_FILE = Info.plist;
238+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
239+
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
240+
PRODUCT_NAME = "$(TARGET_NAME)";
241+
SWIFT_VERSION = 3.0;
242+
};
243+
name = Release;
244+
};
245+
/* End XCBuildConfiguration section */
246+
247+
/* Begin XCConfigurationList section */
248+
7B2BBC6C1C779D710067B71D /* Build configuration list for PBXProject "Tests" */ = {
249+
isa = XCConfigurationList;
250+
buildConfigurations = (
251+
7B2BBC871C779D720067B71D /* Debug */,
252+
7B2BBC881C779D720067B71D /* Release */,
253+
);
254+
defaultConfigurationIsVisible = 0;
255+
defaultConfigurationName = Release;
256+
};
257+
7B2BBC8C1C779D720067B71D /* Build configuration list for PBXNativeTarget "Tests" */ = {
258+
isa = XCConfigurationList;
259+
buildConfigurations = (
260+
7B2BBC8D1C779D720067B71D /* Debug */,
261+
7B2BBC8E1C779D720067B71D /* Release */,
262+
);
263+
defaultConfigurationIsVisible = 0;
264+
defaultConfigurationName = Release;
265+
};
266+
/* End XCConfigurationList section */
267+
};
268+
rootObject = 7B2BBC691C779D710067B71D /* Project object */;
269+
}

Boyer-Moore/Tests/Tests.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)