1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.text.format.cts;
18 
19 import java.math.BigDecimal;
20 import java.math.MathContext;
21 
22 import android.test.AndroidTestCase;
23 import android.text.format.Formatter;
24 
25 public class FormatterTest extends AndroidTestCase {
26 
testFormatFileSize()27     public void testFormatFileSize() {
28         // test null Context
29         assertEquals("", Formatter.formatFileSize(null, 0));
30 
31         MathContext mc = MathContext.DECIMAL64;
32         BigDecimal bd = new BigDecimal((long) 1024, mc);
33 
34         // test different long values with various length
35         assertEquals("0.00 B", Formatter.formatFileSize(mContext, 0));
36 
37         assertEquals("899 B", Formatter.formatFileSize(mContext, 899));
38 
39         assertEquals("1.00 KB", Formatter.formatFileSize(mContext, bd.pow(1).longValue()));
40 
41         assertEquals("1.00 MB", Formatter.formatFileSize(mContext, bd.pow(2).longValue()));
42 
43         assertEquals("1.00 GB", Formatter.formatFileSize(mContext, bd.pow(3).longValue()));
44 
45         assertEquals("1.00 TB", Formatter.formatFileSize(mContext, bd.pow(4).longValue()));
46 
47         assertEquals("1.00 PB", Formatter.formatFileSize(mContext, bd.pow(5).longValue()));
48 
49         assertEquals("1024 PB", Formatter.formatFileSize(mContext, bd.pow(6).longValue()));
50 
51         // test Negative value
52         assertEquals("-1.00 B", Formatter.formatFileSize(mContext, -1));
53     }
54 
testFormatIpAddress()55     public void testFormatIpAddress() {
56         assertEquals("1.0.168.192", Formatter.formatIpAddress(0xC0A80001));
57         assertEquals("1.0.0.127", Formatter.formatIpAddress(0x7F000001));
58         assertEquals("35.182.168.192", Formatter.formatIpAddress(0xC0A8B623));
59         assertEquals("0.255.255.255", Formatter.formatIpAddress(0xFFFFFF00));
60         assertEquals("222.5.15.10", Formatter.formatIpAddress(0x0A0F05DE));
61     }
62 }
63