mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 07:37:25 +00:00

committed by
Moxie Marlinspike

parent
e97255a17f
commit
06e137aee9
@@ -0,0 +1,36 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BitmapUtilTest {
|
||||
@Test public void testScaleFactorNormal() {
|
||||
assertEquals(1, BitmapUtil.getScaleFactor(1000, 1000, 9000, 9000, false));
|
||||
|
||||
assertEquals(1, BitmapUtil.getScaleFactor(1000, 1000, 750, 750, false));
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 1000, 500, 500, false));
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 1000, 499, 499, false));
|
||||
assertEquals(4, BitmapUtil.getScaleFactor(1000, 1000, 250, 250, false));
|
||||
assertEquals(4, BitmapUtil.getScaleFactor(1000, 1000, 249, 249, false));
|
||||
|
||||
assertEquals(1, BitmapUtil.getScaleFactor(1000, 500, 750, 750, false));
|
||||
assertEquals(1, BitmapUtil.getScaleFactor(2000, 1000, 501, 501, false));
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(2000, 1000, 500, 500, false));
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 2000, 499, 499, false));
|
||||
}
|
||||
|
||||
@Test public void testScaleFactorConstrained() {
|
||||
assertEquals(1, BitmapUtil.getScaleFactor(1000, 1000, 9000, 9000, true));
|
||||
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 1000, 750, 750, true));
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 1000, 500, 500, true));
|
||||
assertEquals(4, BitmapUtil.getScaleFactor(1000, 1000, 499, 499, true));
|
||||
assertEquals(4, BitmapUtil.getScaleFactor(1000, 1000, 250, 250, true));
|
||||
assertEquals(8, BitmapUtil.getScaleFactor(1000, 1000, 249, 249, true));
|
||||
|
||||
assertEquals(2, BitmapUtil.getScaleFactor(1000, 500, 750, 750, true));
|
||||
assertEquals(4, BitmapUtil.getScaleFactor(2000, 1000, 500, 500, true));
|
||||
assertEquals(8, BitmapUtil.getScaleFactor(1000, 2000, 499, 499, true));
|
||||
}
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ListPartitionTest {
|
||||
|
||||
@Test public void testPartitionEven() {
|
||||
List<Integer> list = new LinkedList<>();
|
||||
|
||||
for (int i=0;i<100;i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
List<List<Integer>> partitions = Util.partition(list, 10);
|
||||
|
||||
assertEquals(partitions.size(), 10);
|
||||
|
||||
int counter = 0;
|
||||
|
||||
for (int i=0;i<partitions.size();i++) {
|
||||
List<Integer> partition = partitions.get(i);
|
||||
assertEquals(partition.size(), 10);
|
||||
|
||||
for (int j=0;j<partition.size();j++) {
|
||||
assertEquals((int)partition.get(j), counter++);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void testPartitionOdd() {
|
||||
List<Integer> list = new LinkedList<>();
|
||||
|
||||
for (int i=0;i<100;i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
list.add(100);
|
||||
|
||||
List<List<Integer>> partitions = Util.partition(list, 10);
|
||||
|
||||
assertEquals(partitions.size(), 11);
|
||||
|
||||
int counter = 0;
|
||||
|
||||
for (int i=0;i<partitions.size()-1;i++) {
|
||||
List<Integer> partition = partitions.get(i);
|
||||
assertEquals(partition.size(), 10);
|
||||
|
||||
for (int j=0;j<partition.size();j++) {
|
||||
assertEquals((int)partition.get(j), counter++);
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(partitions.get(10).size(), 1);
|
||||
assertEquals((int)partitions.get(10).get(0), 100);
|
||||
}
|
||||
|
||||
@Test public void testPathological() {
|
||||
List<Integer> list = new LinkedList<>();
|
||||
|
||||
for (int i=0;i<100;i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
List<List<Integer>> partitions = Util.partition(list, 1);
|
||||
|
||||
assertEquals(partitions.size(), 100);
|
||||
|
||||
int counter = 0;
|
||||
|
||||
for (int i=0;i<partitions.size();i++) {
|
||||
List<Integer> partition = partitions.get(i);
|
||||
assertEquals(partition.size(), 1);
|
||||
|
||||
for (int j=0;j<partition.size();j++) {
|
||||
assertEquals((int)partition.get(j), counter++);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
||||
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PhoneNumberFormatterTest {
|
||||
private static final String LOCAL_NUMBER = "+15555555555";
|
||||
|
||||
@Test public void testFormatNumberE164() throws Exception, InvalidNumberException {
|
||||
assertThat(PhoneNumberFormatter.formatNumber("(555) 555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
|
||||
assertThat(PhoneNumberFormatter.formatNumber("555-5555", LOCAL_NUMBER)).isEqualTo(LOCAL_NUMBER);
|
||||
assertThat(PhoneNumberFormatter.formatNumber("(123) 555-5555", LOCAL_NUMBER)).isNotEqualTo(LOCAL_NUMBER);
|
||||
}
|
||||
|
||||
@Test public void testFormatNumberEmail() throws Exception {
|
||||
try {
|
||||
PhoneNumberFormatter.formatNumber("person@domain.com", LOCAL_NUMBER);
|
||||
throw new AssertionFailedError("should have thrown on email");
|
||||
} catch (InvalidNumberException ine) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Open Whisper Systems
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class Rfc5724UriTest {
|
||||
|
||||
private static final String TAG = Rfc5724UriTest.class.getSimpleName();
|
||||
|
||||
@Test public void testInvalidPath() throws Exception {
|
||||
final String[] invalidSchemaUris = {
|
||||
"",
|
||||
":",
|
||||
"sms:",
|
||||
":sms",
|
||||
"sms:?goto=fail",
|
||||
"sms:?goto=fail&fail=goto"
|
||||
};
|
||||
|
||||
for (String uri : invalidSchemaUris) {
|
||||
try {
|
||||
new Rfc5724Uri(uri);
|
||||
throw new AssertionFailedError("URISyntaxException should be thrown");
|
||||
} catch (URISyntaxException e) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void testGetSchema() throws Exception {
|
||||
final String[][] uriTestPairs = {
|
||||
{"sms:+15555555555", "sms"},
|
||||
{"sMs:+15555555555", "sMs"},
|
||||
{"smsto:+15555555555?", "smsto"},
|
||||
{"mms:+15555555555?a=b", "mms"},
|
||||
{"mmsto:+15555555555?a=b&c=d", "mmsto"}
|
||||
};
|
||||
|
||||
for (String[] uriTestPair : uriTestPairs) {
|
||||
final Rfc5724Uri testUri = new Rfc5724Uri(uriTestPair[0]);
|
||||
assertTrue(testUri.getSchema().equals(uriTestPair[1]));
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void testGetPath() throws Exception {
|
||||
final String[][] uriTestPairs = {
|
||||
{"sms:+15555555555", "+15555555555"},
|
||||
{"sms:%2B555555555", "%2B555555555"},
|
||||
{"smsto:+15555555555?", "+15555555555"},
|
||||
{"mms:+15555555555?a=b", "+15555555555"},
|
||||
{"mmsto:+15555555555?a=b&c=d", "+15555555555"},
|
||||
{"sms:+15555555555,+14444444444", "+15555555555,+14444444444"},
|
||||
{"sms:+15555555555,+14444444444?", "+15555555555,+14444444444"},
|
||||
{"sms:+15555555555,+14444444444?a=b", "+15555555555,+14444444444"},
|
||||
{"sms:+15555555555,+14444444444?a=b&c=d", "+15555555555,+14444444444"}
|
||||
};
|
||||
|
||||
for (String[] uriTestPair : uriTestPairs) {
|
||||
final Rfc5724Uri testUri = new Rfc5724Uri(uriTestPair[0]);
|
||||
assertTrue(testUri.getPath().equals(uriTestPair[1]));
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void testGetQueryParams() throws Exception {
|
||||
final String[][] uriTestPairs = {
|
||||
{"sms:+15555555555", "a", null},
|
||||
{"mms:+15555555555?b=", "a", null},
|
||||
{"mmsto:+15555555555?a=", "a", ""},
|
||||
{"sms:+15555555555?a=b", "a", "b"},
|
||||
{"sms:+15555555555?a=b&c=d", "a", "b"},
|
||||
{"sms:+15555555555?a=b&c=d", "b", null},
|
||||
{"sms:+15555555555?a=b&c=d", "c", "d"},
|
||||
{"sms:+15555555555?a=b&c=d", "d", null}
|
||||
};
|
||||
|
||||
for (String[] uriTestPair : uriTestPairs) {
|
||||
final Rfc5724Uri testUri = new Rfc5724Uri(uriTestPair[0]);
|
||||
final String paramResult = testUri.getQueryParams().get(uriTestPair[1]);
|
||||
|
||||
Log.d(TAG, paramResult + " ?= " + uriTestPair[2]);
|
||||
if (paramResult == null) assertTrue(uriTestPair[2] == null);
|
||||
else assertTrue(paramResult.equals(uriTestPair[2]));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user