浏览代码

Add support for terabyte and petabyte memory units. (#138)

* Add support for terabyte and petabyte memory units.

This fixes #137 (but not #136, this will come separately once we decide
what we want to do if anything).

* Add a test.

* Update copyright year
Alexey Alexandrov 8 年前
父节点
当前提交
c0fb62ec88
共有 2 个文件被更改,包括 59 次插入4 次删除
  1. 16
    4
      internal/measurement/measurement.go
  2. 43
    0
      internal/measurement/measurement_test.go

+ 16
- 4
internal/measurement/measurement.go 查看文件

170
 
170
 
171
 	switch fromUnit {
171
 	switch fromUnit {
172
 	case "byte", "b":
172
 	case "byte", "b":
173
-	case "kilobyte", "kb":
173
+	case "kb", "kbyte", "kilobyte":
174
 		value *= 1024
174
 		value *= 1024
175
-	case "megabyte", "mb":
175
+	case "mb", "mbyte", "megabyte":
176
 		value *= 1024 * 1024
176
 		value *= 1024 * 1024
177
-	case "gigabyte", "gb":
177
+	case "gb", "gbyte", "gigabyte":
178
 		value *= 1024 * 1024 * 1024
178
 		value *= 1024 * 1024 * 1024
179
+	case "tb", "tbyte", "terabyte":
180
+		value *= 1024 * 1024 * 1024 * 1024
181
+	case "pb", "pbyte", "petabyte":
182
+		value *= 1024 * 1024 * 1024 * 1024 * 1024
179
 	default:
183
 	default:
180
 		return 0, "", false
184
 		return 0, "", false
181
 	}
185
 	}
188
 			toUnit = "kb"
192
 			toUnit = "kb"
189
 		case value < 1024*1024*1024:
193
 		case value < 1024*1024*1024:
190
 			toUnit = "mb"
194
 			toUnit = "mb"
191
-		default:
195
+		case value < 1024*1024*1024*1024:
192
 			toUnit = "gb"
196
 			toUnit = "gb"
197
+		case value < 1024*1024*1024*1024*1024:
198
+			toUnit = "tb"
199
+		default:
200
+			toUnit = "pb"
193
 		}
201
 		}
194
 	}
202
 	}
195
 
203
 
203
 		output, toUnit = float64(value)/(1024*1024), "MB"
211
 		output, toUnit = float64(value)/(1024*1024), "MB"
204
 	case "gb", "gbyte", "gigabyte":
212
 	case "gb", "gbyte", "gigabyte":
205
 		output, toUnit = float64(value)/(1024*1024*1024), "GB"
213
 		output, toUnit = float64(value)/(1024*1024*1024), "GB"
214
+	case "tb", "tbyte", "terabyte":
215
+		output, toUnit = float64(value)/(1024*1024*1024*1024), "TB"
216
+	case "pb", "pbyte", "petabyte":
217
+		output, toUnit = float64(value)/(1024*1024*1024*1024*1024), "PB"
206
 	}
218
 	}
207
 	return output, toUnit, true
219
 	return output, toUnit, true
208
 }
220
 }

+ 43
- 0
internal/measurement/measurement_test.go 查看文件

1
+// Copyright 2017 Google Inc. All Rights Reserved.
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+//     http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package measurement
16
+
17
+import (
18
+	"testing"
19
+)
20
+
21
+func TestScale(t *testing.T) {
22
+	for _, tc := range []struct {
23
+		value            int64
24
+		fromUnit, toUnit string
25
+		wantValue        float64
26
+		wantUnit         string
27
+	}{
28
+		{1, "s", "ms", 1000, "ms"},
29
+		{1, "kb", "b", 1024, "B"},
30
+		{1, "kbyte", "b", 1024, "B"},
31
+		{1, "kilobyte", "b", 1024, "B"},
32
+		{1, "mb", "kb", 1024, "kB"},
33
+		{1, "gb", "mb", 1024, "MB"},
34
+		{1024, "gb", "tb", 1, "TB"},
35
+		{1024, "tb", "pb", 1, "PB"},
36
+		{2048, "mb", "auto", 2, "GB"},
37
+	} {
38
+		if gotValue, gotUnit := Scale(tc.value, tc.fromUnit, tc.toUnit); gotValue != tc.wantValue || gotUnit != tc.wantUnit {
39
+			t.Errorf("Scale(%d, %q, %q) = (%f, %q), want (%f, %q)",
40
+				tc.value, tc.fromUnit, tc.toUnit, gotValue, gotUnit, tc.wantValue, tc.wantUnit)
41
+		}
42
+	}
43
+}