Sha256: f8feb7b00395dc4c8c55cddd4d5d878d801ca34fa6fd36d45a2169be9bce8682

Contents?: true

Size: 1.78 KB

Versions: 376

Compression:

Stored size: 1.78 KB

Contents

import
 critbits, unittest
import wordcount

proc `==`(a, b: TWordCount): bool =
  if a.len != b.len:
    return
  for key, count in a:
    if b[key] != count:
      return
  return true

proc fromPairs(pairs: seq[tuple[word: string, count: int]]): TWordCount =
  for p in pairs:
    result[p.word] = p.count

test "count one word":
  let
    expected = fromPairs(@[("word", 1)])
    result = wordCount("word")
  check result == expected

test "count one of each":
  let
    expected = fromPairs(@[("one", 1), ("of", 1), ("each", 1)])
    result = wordCount("one of each")
  check result == expected

test "count multiple occurrences":
  let
    expected = fromPairs(@[("one", 1), ("fish", 4), ("two", 1),
                           ("red", 1), ("blue", 1)])
    result = wordCount("one fish two fish red fish blue fish")
  check result == expected

test "ignore punctuation":
  let
    expected = fromPairs(@[("car", 1), ("carpet", 1), ("as", 1),
                           ("java", 1), ("javascript", 1)])
    result = wordCount("car : carpet as java : javascript!!&@$%^&")
  check result == expected

test "include numbers":
  let
    expected = fromPairs(@[("testing", 2), ("1", 1), ("2", 1)])
    result = wordCount("testing, 1, 2 testing")
  check result == expected

test "normalize case":
  let
    result = wordCount("go Go GO")
    expected = fromPairs(@[("go", 3)])
  check result == expected

test "prefix punctuation":
  let
    expected = fromPairs(@[("testing", 2), ("1", 1), ("2", 1)])
    result = wordCount("!%%#testing, 1, 2 testing")
  check result == expected

test "symbols are separators":
  let
    expected = fromPairs(@[("hey", 1), ("my", 1), ("spacebar", 1),
                           ("is", 1), ("broken", 1)])
    result = wordCount("hey,my_spacebar_is_broken.")
  check result == expected

Version data entries

376 entries across 376 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.77 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.76 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.75 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.74 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.73 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.72 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.71 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.70 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.69 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.68 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.67 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.66 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.65 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.64 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.63 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.62 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.61 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.60 tracks/nim/exercises/word-count/word_count_test.nim
trackler-2.2.1.59 tracks/nim/exercises/word-count/word_count_test.nim