-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_records.rb
50 lines (43 loc) · 1.45 KB
/
make_records.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
require 'csv'
require 'minitest/autorun'
require 'test/unit'
# records are csv file
#
# in irb
#
# >> a = ['a', 'b', 'c']
# => ["a", "b", "c"]
# >> a.each_with_index {|r,i| p [i,r].to_csv}
# "0,a\n"
# "1,b\n"
# "2,c\n"
# => ["a", "b", "c"]
def gen_random_string(len)
(0...len).map { rand(36).to_s(36) }.join
end
def make_records(numbers, filename)
numbers, filename = ARGV[0], ARGV[1]
records = []
numbers.to_i.times { records << gen_random_string(10) + ' ' + gen_random_string(10) }
csv = ''
records.each_with_index do |text, index|
csv << [index, text].to_csv
end
File.write("#{filename}.csv", csv)
end
make_records(ARGV[0], ARGV[1]) if __FILE__ == $PROGRAM_NAME
# testing
class TestAddRecordSeparator < Test::Unit::TestCase
def test_gen_random_string
# 正确的测试方法是从概率上考察,随机100次,不相同的应该占大多数
# 虽然从概率上说100次完全相同也是允许的 :(
# http://stackoverflow.com/questions/2082970/whats-the-best-way-to-test-this
assert_equal 10, gen_random_string(10).length, '是否生成指定长度的字符串'
assert_not_equal gen_random_string(10), gen_random_string(10)
assert_not_equal gen_random_string(10), gen_random_string(10)
assert_not_equal gen_random_string(10), gen_random_string(10)
assert_not_equal gen_random_string(10), gen_random_string(10)
assert_not_equal gen_random_string(10), gen_random_string(10)
end
end