Skip to content

Commit 00bc111

Browse files
committed
Tests
1 parent dc243f6 commit 00bc111

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

lib/std/ascii.zig

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! See also: https://en.wikipedia.org/wiki/ASCII#Character_set
1010

1111
const std = @import("std");
12+
const testing = std.testing;
1213

1314
/// Contains constants for the C0 control codes of the ASCII encoding.
1415
///
@@ -213,7 +214,6 @@ pub fn isSpace(c: u8) bool {
213214
pub const spaces = [_]u8{ ' ', '\t', '\n', '\r', control_code.VT, control_code.FF };
214215

215216
test "spaces" {
216-
const testing = std.testing;
217217
for (spaces) |space| try testing.expect(isSpace(space));
218218

219219
var i: u8 = 0;
@@ -259,15 +259,50 @@ pub fn toLower(c: u8) u8 {
259259
}
260260

261261
test "ascii character classes" {
262-
const testing = std.testing;
262+
try testing.expect(!isCntrl('a'));
263+
try testing.expect(!isCntrl('z'));
264+
try testing.expect(isCntrl(control_code.NUL));
265+
try testing.expect(isCntrl(control_code.FF));
266+
try testing.expect(isCntrl(control_code.US));
263267

264268
try testing.expect('C' == toUpper('c'));
265269
try testing.expect(':' == toUpper(':'));
266270
try testing.expect('\xab' == toUpper('\xab'));
271+
try testing.expect(!isUpper('z'));
272+
267273
try testing.expect('c' == toLower('C'));
274+
try testing.expect(':' == toLower(':'));
275+
try testing.expect('\xab' == toLower('\xab'));
276+
try testing.expect(!isLower('Z'));
277+
278+
try testing.expect(isAlNum('Z'));
279+
try testing.expect(isAlNum('z'));
280+
try testing.expect(isAlNum('5'));
281+
try testing.expect(isAlNum('5'));
282+
try testing.expect(!isAlNum('!'));
283+
284+
try testing.expect(!isAlpha('5'));
268285
try testing.expect(isAlpha('c'));
269286
try testing.expect(!isAlpha('5'));
287+
270288
try testing.expect(isSpace(' '));
289+
try testing.expect(isSpace('\t'));
290+
try testing.expect(isSpace('\r'));
291+
try testing.expect(isSpace('\n'));
292+
try testing.expect(!isSpace('.'));
293+
294+
try testing.expect(!isXDigit('g'));
295+
try testing.expect(isXDigit('b'));
296+
try testing.expect(isXDigit('9'));
297+
298+
try testing.expect(!isDigit('~'));
299+
try testing.expect(isDigit('0'));
300+
try testing.expect(isDigit('9'));
301+
302+
try testing.expect(isPrint(' '));
303+
try testing.expect(isPrint('@'));
304+
try testing.expect(isPrint('~'));
305+
try testing.expect(!isPrint(control_code.ESC));
271306
}
272307

273308
/// Writes a lower case copy of `ascii_string` to `output`.
@@ -283,7 +318,7 @@ pub fn lowerString(output: []u8, ascii_string: []const u8) []u8 {
283318
test "lowerString" {
284319
var buf: [1024]u8 = undefined;
285320
const result = lowerString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
286-
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
321+
try testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
287322
}
288323

289324
/// Allocates a lower case copy of `ascii_string`.
@@ -294,9 +329,9 @@ pub fn allocLowerString(allocator: std.mem.Allocator, ascii_string: []const u8)
294329
}
295330

296331
test "allocLowerString" {
297-
const result = try allocLowerString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
298-
defer std.testing.allocator.free(result);
299-
try std.testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
332+
const result = try allocLowerString(testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
333+
defer testing.allocator.free(result);
334+
try testing.expectEqualStrings("abcdefghijklmnopqrst0234+💩!", result);
300335
}
301336

302337
/// Writes an upper case copy of `ascii_string` to `output`.
@@ -312,7 +347,7 @@ pub fn upperString(output: []u8, ascii_string: []const u8) []u8 {
312347
test "upperString" {
313348
var buf: [1024]u8 = undefined;
314349
const result = upperString(&buf, "aBcDeFgHiJkLmNOPqrst0234+💩!");
315-
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
350+
try testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
316351
}
317352

318353
/// Allocates an upper case copy of `ascii_string`.
@@ -323,12 +358,12 @@ pub fn allocUpperString(allocator: std.mem.Allocator, ascii_string: []const u8)
323358
}
324359

325360
test "allocUpperString" {
326-
const result = try allocUpperString(std.testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
327-
defer std.testing.allocator.free(result);
328-
try std.testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
361+
const result = try allocUpperString(testing.allocator, "aBcDeFgHiJkLmNOPqrst0234+💩!");
362+
defer testing.allocator.free(result);
363+
try testing.expectEqualStrings("ABCDEFGHIJKLMNOPQRST0234+💩!", result);
329364
}
330365

331-
/// Compares strings `a` and `b` case insensitively and returns whether they are equal.
366+
/// Compares strings `a` and `b` case-insensitively and returns whether they are equal.
332367
pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
333368
if (a.len != b.len) return false;
334369
for (a) |a_c, i| {
@@ -338,27 +373,27 @@ pub fn eqlIgnoreCase(a: []const u8, b: []const u8) bool {
338373
}
339374

340375
test "eqlIgnoreCase" {
341-
try std.testing.expect(eqlIgnoreCase("HEl💩Lo!", "hel💩lo!"));
342-
try std.testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
343-
try std.testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
376+
try testing.expect(eqlIgnoreCase("HEl💩Lo!", "hel💩lo!"));
377+
try testing.expect(!eqlIgnoreCase("hElLo!", "hello! "));
378+
try testing.expect(!eqlIgnoreCase("hElLo!", "helro!"));
344379
}
345380

346381
pub fn startsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
347382
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[0..needle.len], needle);
348383
}
349384

350385
test "ascii.startsWithIgnoreCase" {
351-
try std.testing.expect(startsWithIgnoreCase("boB", "Bo"));
352-
try std.testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
386+
try testing.expect(startsWithIgnoreCase("boB", "Bo"));
387+
try testing.expect(!startsWithIgnoreCase("Needle in hAyStAcK", "haystack"));
353388
}
354389

355390
pub fn endsWithIgnoreCase(haystack: []const u8, needle: []const u8) bool {
356391
return if (needle.len > haystack.len) false else eqlIgnoreCase(haystack[haystack.len - needle.len ..], needle);
357392
}
358393

359394
test "ascii.endsWithIgnoreCase" {
360-
try std.testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
361-
try std.testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
395+
try testing.expect(endsWithIgnoreCase("Needle in HaYsTaCk", "haystack"));
396+
try testing.expect(!endsWithIgnoreCase("BoB", "Bo"));
362397
}
363398

364399
/// Finds `substr` in `container`, ignoring case, starting at `start_index`.
@@ -380,12 +415,11 @@ pub fn indexOfIgnoreCase(container: []const u8, substr: []const u8) ?usize {
380415
}
381416

382417
test "indexOfIgnoreCase" {
383-
try std.testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
384-
try std.testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
385-
try std.testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
386-
try std.testing.expect(indexOfIgnoreCase("foo", "fool") == null);
387-
388-
try std.testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
418+
try testing.expect(indexOfIgnoreCase("one Two Three Four", "foUr").? == 14);
419+
try testing.expect(indexOfIgnoreCase("one two three FouR", "gOur") == null);
420+
try testing.expect(indexOfIgnoreCase("foO", "Foo").? == 0);
421+
try testing.expect(indexOfIgnoreCase("foo", "fool") == null);
422+
try testing.expect(indexOfIgnoreCase("FOO foo", "fOo").? == 0);
389423
}
390424

391425
/// Compares two slices of numbers lexicographically. O(n).

0 commit comments

Comments
 (0)