Skip to content

Commit

Permalink
added the rest of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mk2s committed Feb 17, 2024
1 parent 0cd5fa1 commit fc2c89a
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 3 deletions.
40 changes: 37 additions & 3 deletions exercises/practice/roman-numerals/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
const std = @import("std");
const mem = std.mem;
const allocPrint = std.fmt.allocPrint;

const DigitValue = struct {
value: i16,
digit: []const u8,
};

const max_result_size = 10;

var digitValues = [_]DigitValue{
.{ .value = 1000, .digit = "M" },
.{ .value = 900, .digit = "CM" },
.{ .value = 500, .digit = "D" },
.{ .value = 400, .digit = "CD" },
.{ .value = 100, .digit = "C" },
.{ .value = 90, .digit = "XC" },
.{ .value = 50, .digit = "L" },
.{ .value = 40, .digit = "XL" },
.{ .value = 10, .digit = "X" },
.{ .value = 9, .digit = "IX" },
.{ .value = 5, .digit = "V" },
.{ .value = 4, .digit = "IV" },
.{ .value = 1, .digit = "I" },
};

pub fn toRoman(allocator: mem.Allocator, arabicNumeral: i16) mem.Allocator.Error![]u8 {
_ = allocator;
_ = arabicNumeral;
@compileError("please implement the toRoman function");
var tmp: [10]u8 = undefined;
var numeral: i16 = arabicNumeral;
var len: usize = 0;
for (digitValues) |dv| {
while (numeral >= dv.value) {
std.mem.copy(u8, tmp[len..], dv.digit);
len += dv.digit.len;
numeral -= dv.value;
}
}
var result = try allocator.alloc(u8, len);
std.mem.copy(u8, result, tmp[0..len]);
return result;
}
175 changes: 175 additions & 0 deletions exercises/practice/roman-numerals/test_roman_numerals.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,178 @@ test "1 is I" {
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "2 is II" {
const expected = "II";
const actual = try toRoman(testing.allocator, 2);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "3 is III" {
const expected = "III";
const actual = try toRoman(testing.allocator, 3);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "4 is IV" {
const expected = "IV";
const actual = try toRoman(testing.allocator, 4);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "5 is V" {
const expected = "V";
const actual = try toRoman(testing.allocator, 5);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "6 is VI" {
const expected = "VI";
const actual = try toRoman(testing.allocator, 6);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "9 is IX" {
const expected = "IX";
const actual = try toRoman(testing.allocator, 9);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "16 is XVI" {
const expected = "XVI";
const actual = try toRoman(testing.allocator, 16);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "27 is XXVII" {
const expected = "XXVII";
const actual = try toRoman(testing.allocator, 27);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "48 is XLVIII" {
const expected = "XLVIII";
const actual = try toRoman(testing.allocator, 48);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "49 is XLIX" {
const expected = "XLIX";
const actual = try toRoman(testing.allocator, 49);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "59 is LIX" {
const expected = "LIX";
const actual = try toRoman(testing.allocator, 59);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "66 is LXVI" {
const expected = "LXVI";
const actual = try toRoman(testing.allocator, 66);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "93 is XCIII" {
const expected = "XCIII";
const actual = try toRoman(testing.allocator, 93);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "141 is CXLI" {
const expected = "CXLI";
const actual = try toRoman(testing.allocator, 141);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "163 is CLXIII" {
const expected = "CLXIII";
const actual = try toRoman(testing.allocator, 163);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "166 is CLXVI" {
const expected = "CLXVI";
const actual = try toRoman(testing.allocator, 166);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "402 is CDII" {
const expected = "CDII";
const actual = try toRoman(testing.allocator, 402);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "575 is DLXXV" {
const expected = "DLXXV";
const actual = try toRoman(testing.allocator, 575);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "666 is DCLXVI" {
const expected = "DCLXVI";
const actual = try toRoman(testing.allocator, 666);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "911 is CMXI" {
const expected = "CMXI";
const actual = try toRoman(testing.allocator, 911);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "1024 is MXXIV" {
const expected = "MXXIV";
const actual = try toRoman(testing.allocator, 1024);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "1666 is MDCLXVI" {
const expected = "MDCLXVI";
const actual = try toRoman(testing.allocator, 1666);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "3000 is MMM" {
const expected = "MMM";
const actual = try toRoman(testing.allocator, 3000);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "3001 is MMMI" {
const expected = "MMMI";
const actual = try toRoman(testing.allocator, 3001);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

test "3999 is MMMCMXCIX" {
const expected = "MMMCMXCIX";
const actual = try toRoman(testing.allocator, 3999);
defer testing.allocator.free(actual);
try testing.expectEqualStrings(expected, actual);
}

0 comments on commit fc2c89a

Please sign in to comment.