|
180 | 180 | end |
181 | 181 | end |
182 | 182 |
|
| 183 | + describe '.write_csv' do |
| 184 | + it 'writes CSV file with metadata and table data' do |
| 185 | + Dir.mktmpdir do |dir| |
| 186 | + output_path = File.join(dir, 'output_001') |
| 187 | + ruby_descriptions = { |
| 188 | + 'ruby-base' => 'ruby 3.3.0', |
| 189 | + 'ruby-yjit' => 'ruby 3.3.0 +YJIT' |
| 190 | + } |
| 191 | + table = [ |
| 192 | + ['Benchmark', 'ruby-base', 'ruby-yjit'], |
| 193 | + ['fib', '1.5', '1.0'], |
| 194 | + ['matmul', '2.0', '1.8'] |
| 195 | + ] |
| 196 | + |
| 197 | + result_path = BenchmarkRunner.write_csv(output_path, ruby_descriptions, table) |
| 198 | + |
| 199 | + expected_path = File.join(dir, 'output_001.csv') |
| 200 | + assert_equal expected_path, result_path |
| 201 | + assert File.exist?(expected_path) |
| 202 | + |
| 203 | + csv_rows = CSV.read(expected_path) |
| 204 | + assert_equal 'ruby-base', csv_rows[0][0] |
| 205 | + assert_equal 'ruby 3.3.0', csv_rows[0][1] |
| 206 | + assert_equal 'ruby-yjit', csv_rows[1][0] |
| 207 | + assert_equal 'ruby 3.3.0 +YJIT', csv_rows[1][1] |
| 208 | + assert_equal [], csv_rows[2] |
| 209 | + assert_equal table, csv_rows[3..5] |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 213 | + it 'returns the CSV file path' do |
| 214 | + Dir.mktmpdir do |dir| |
| 215 | + output_path = File.join(dir, 'output_test') |
| 216 | + result_path = BenchmarkRunner.write_csv(output_path, {}, []) |
| 217 | + |
| 218 | + assert_equal File.join(dir, 'output_test.csv'), result_path |
| 219 | + end |
| 220 | + end |
| 221 | + |
| 222 | + it 'handles empty metadata and table' do |
| 223 | + Dir.mktmpdir do |dir| |
| 224 | + output_path = File.join(dir, 'output_empty') |
| 225 | + |
| 226 | + result_path = BenchmarkRunner.write_csv(output_path, {}, []) |
| 227 | + |
| 228 | + assert File.exist?(result_path) |
| 229 | + csv_rows = CSV.read(result_path) |
| 230 | + assert_equal [[]], csv_rows |
| 231 | + end |
| 232 | + end |
| 233 | + |
| 234 | + it 'handles single metadata entry' do |
| 235 | + Dir.mktmpdir do |dir| |
| 236 | + output_path = File.join(dir, 'output_single') |
| 237 | + ruby_descriptions = { 'ruby' => 'ruby 3.3.0' } |
| 238 | + table = [['Benchmark', 'Time'], ['test', '1.0']] |
| 239 | + |
| 240 | + result_path = BenchmarkRunner.write_csv(output_path, ruby_descriptions, table) |
| 241 | + |
| 242 | + csv_rows = CSV.read(result_path) |
| 243 | + assert_equal 'ruby', csv_rows[0][0] |
| 244 | + assert_equal 'ruby 3.3.0', csv_rows[0][1] |
| 245 | + assert_equal [], csv_rows[1] |
| 246 | + assert_equal table, csv_rows[2..3] |
| 247 | + end |
| 248 | + end |
| 249 | + |
| 250 | + it 'preserves precision in numeric strings' do |
| 251 | + Dir.mktmpdir do |dir| |
| 252 | + output_path = File.join(dir, 'output_precision') |
| 253 | + table = [['Benchmark', 'Time'], ['test', '1.234567890123']] |
| 254 | + |
| 255 | + result_path = BenchmarkRunner.write_csv(output_path, {}, table) |
| 256 | + |
| 257 | + csv_rows = CSV.read(result_path) |
| 258 | + assert_equal '1.234567890123', csv_rows[2][1] |
| 259 | + end |
| 260 | + end |
| 261 | + |
| 262 | + it 'overwrites existing CSV file' do |
| 263 | + Dir.mktmpdir do |dir| |
| 264 | + output_path = File.join(dir, 'output_overwrite') |
| 265 | + |
| 266 | + # Write first version |
| 267 | + BenchmarkRunner.write_csv(output_path, { 'v1' => 'first' }, [['old']]) |
| 268 | + |
| 269 | + # Write second version |
| 270 | + new_descriptions = { 'v2' => 'second' } |
| 271 | + new_table = [['new']] |
| 272 | + result_path = BenchmarkRunner.write_csv(output_path, new_descriptions, new_table) |
| 273 | + |
| 274 | + csv_rows = CSV.read(result_path) |
| 275 | + assert_equal 'v2', csv_rows[0][0] |
| 276 | + assert_equal 'second', csv_rows[0][1] |
| 277 | + end |
| 278 | + end |
| 279 | + end |
| 280 | + |
183 | 281 | describe '.write_json' do |
184 | 282 | it 'writes JSON file with metadata and raw data' do |
185 | 283 | Dir.mktmpdir do |dir| |
|
0 commit comments