2010-12-15 Francois Chouinard <fchouinard@gmail.com> Fix for Bug332680
authorFrancois Chouinard <fchouinard@gmail.com>
Wed, 15 Dec 2010 20:18:33 +0000 (20:18 +0000)
committerFrancois Chouinard <fchouinard@gmail.com>
Wed, 15 Dec 2010 20:18:33 +0000 (20:18 +0000)
* src/org/eclipse/linuxtools/lttng/event/LttngTimestamp.java: Fixed a rounding error
* src/org/eclipse/linuxtools/lttng/tests/event/LttngTimestampTest.java: Added a proper test for toString()

org.eclipse.linuxtools.lttng.tests/ChangeLog
org.eclipse.linuxtools.lttng.tests/src/org/eclipse/linuxtools/lttng/tests/event/LttngTimestampTest.java
org.eclipse.linuxtools.lttng/ChangeLog
org.eclipse.linuxtools.lttng/src/org/eclipse/linuxtools/lttng/event/LttngTimestamp.java

index 5177c70e13856e262c96108d670a41370e05dd99..4202a323aa62f9d87146398adb5ee867f9ff4d65 100644 (file)
@@ -1,3 +1,7 @@
+2010-12-15  Francois Chouinard  <fchouinard@gmail.com>
+
+       * src/org/eclipse/linuxtools/lttng/tests/event/LttngTimestampTest.java: Added a proper test for toString()
+
 2010-11-23  Francois Chouinard  <fchouinard@gmail.com>  Adjusted tests to new API
 
        * src/org/eclipse/linuxtools/lttng/state/experiment/StateExperimentManagerTextTest.java: 
index d2c9bdc8f2110a053d89f4d36bd2f9d54127b515..95c2509b07cd970d1d44283755c6878d913268b3 100644 (file)
@@ -14,44 +14,44 @@ import org.eclipse.linuxtools.tmf.trace.TmfContext;
 
 /*
  Functions tested here :
   public LttngTimestamp()
   public LttngTimestamp(long newEventTime)
   public LttngTimestamp(TmfTimestamp oldEventTime)
-    
   public long getValue()
   public String getSeconds()
   public String getNanoSeconds()
-    
   public void setValue(long newValue)
-    
   public String toString()
+ public LttngTimestamp()
+ public LttngTimestamp(long newEventTime)
+ public LttngTimestamp(TmfTimestamp oldEventTime)
+
+ public long getValue()
+ public String getSeconds()
+ public String getNanoSeconds()
+
+ public void setValue(long newValue)
+
+ public String toString()
  */
 
 @SuppressWarnings("nls")
 public class LttngTimestampTest extends TestCase {
-       private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt";
-    private final static boolean skipIndexing=true;
-    
-    private final static String firstEventTimeSecond     = "13589";
-    private final static String firstEventTimeNano       = "759412128";
-    private final static long   firstEventTimeFull       = 13589759412128L;
-    
+    private final static String tracepath1 = "traceset/trace-15316events_nolost_newformat.txt";
+    private final static boolean skipIndexing = true;
+
+    private final static String firstEventTimeSecond = "13589";
+    private final static String firstEventTimeNano   = "759412128";
+    private final static long   firstEventTimeFull   = 13589759412128L;
+
     private static LTTngTextTrace testStream = null;
+
     private LTTngTextTrace initializeEventStream() {
-               if (testStream == null) {
-                       try {
-                               URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null);
-                               File testfile = new File(FileLocator.toFileURL(location).toURI());
-                               LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath(), skipIndexing);
-                               testStream = tmpStream;
-                       } 
-                       catch (Exception e) {
-                               System.out.println("ERROR : Could not open " + tracepath1);
-                               testStream = null;
-                       }
-               }
-               return testStream;
-       }
+        if (testStream == null) {
+            try {
+                URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null);
+                File testfile = new File(FileLocator.toFileURL(location).toURI());
+                LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath(), skipIndexing);
+                testStream = tmpStream;
+            } catch (Exception e) {
+                System.out.println("ERROR : Could not open " + tracepath1);
+                testStream = null;
+            }
+        }
+        return testStream;
+    }
 
     private LttngTimestamp prepareToTest() {
         LttngTimestamp tmpTime = null;
@@ -59,71 +59,85 @@ public class LttngTimestampTest extends TestCase {
         // This trace should be valid
         try {
             LTTngTextTrace tmpStream = initializeEventStream();
-            tmpTime = (LttngTimestamp)tmpStream.getNextEvent( new TmfContext(null, 0) ).getTimestamp();
-        } 
-        catch (Exception e) {
+            tmpTime = (LttngTimestamp) tmpStream.getNextEvent(new TmfContext(null, 0)).getTimestamp();
+        } catch (Exception e) {
             fail("ERROR : Failed to get reference!");
         }
 
         return tmpTime;
     }
-    
+
     public void testConstructors() {
         LttngTimestamp tmpTime = null;
         @SuppressWarnings("unused")
         LttngTimestamp tmpTime2 = null;
-        
+
         // Default construction with no argument
         try {
             tmpTime = new LttngTimestamp();
-        }
-        catch( Exception e) { 
+        } catch (Exception e) {
             fail("Construction failed!");
         }
-        
+
         // Default construction with good argument
         try {
             tmpTime = new LttngTimestamp(1);
-        }
-        catch( Exception e) { 
+        } catch (Exception e) {
             fail("Construction failed!");
         }
-        
+
         // Copy constructor
         try {
             tmpTime = new LttngTimestamp(1);
             tmpTime2 = new LttngTimestamp(tmpTime);
-        }
-        catch( Exception e) { 
+        } catch (Exception e) {
             fail("Construction failed!");
         }
     }
-    
-    
+
     public void testGetter() {
         LttngTimestamp tmpTime = prepareToTest();
-        
-        assertEquals("Time in second is wrong", firstEventTimeSecond, tmpTime.getSeconds() );
-        assertEquals("Time in nano second is wrong", firstEventTimeNano, tmpTime.getNanoSeconds() );
-        
-        assertEquals("Full time is wrong", firstEventTimeFull, tmpTime.getValue() );
+
+        assertEquals("Time in second is wrong", firstEventTimeSecond, tmpTime.getSeconds());
+        assertEquals("Time in nano second is wrong", firstEventTimeNano, tmpTime.getNanoSeconds());
+
+        assertEquals("Full time is wrong", firstEventTimeFull, tmpTime.getValue());
     }
-    
+
     public void testSetter() {
         LttngTimestamp tmpTime = prepareToTest();
-        
+
         // We will set a time and we will make sure the set is working then
         tmpTime.setValue(1);
-        assertEquals("Full time is wrong after set", 1, tmpTime.getValue() );
+        assertEquals("Full time is wrong after set", 1, tmpTime.getValue());
     }
-    
-    
+
     public void testToString() {
         LttngTimestamp tmpTime = prepareToTest();
-        
+
         // Just make sure toString() does not return null or the java reference
-        assertNotSame("toString returned null",null, tmpTime.toString() );
-        assertNotSame("toString is not overridded!", tmpTime.getClass().getName() + '@' + Integer.toHexString(tmpTime.hashCode()), tmpTime.toString() );
+        assertNotSame("toString returned null", null, tmpTime.toString());
+        assertNotSame("toString is not overridded!", tmpTime.getClass().getName() + '@' + Integer.toHexString(tmpTime.hashCode()), tmpTime.toString());
     }
-    
+
+    // Better test...
+    public void testToString2() {
+        LttngTimestamp ts1 = new LttngTimestamp(2064357056377L);
+        String expectedTS1 = "2064.357056377";
+
+        LttngTimestamp ts2 = new LttngTimestamp(1L);
+        String expectedTS2 = "0.000000001";
+
+        LttngTimestamp ts3 = new LttngTimestamp(123456789L);
+        String expectedTS3 = "0.123456789";
+
+        LttngTimestamp ts4 = new LttngTimestamp(1234567890L);
+        String expectedTS4 = "1.234567890";
+
+        assertEquals("toString()", expectedTS1, ts1.toString());
+        assertEquals("toString()", expectedTS2, ts2.toString());
+        assertEquals("toString()", expectedTS3, ts3.toString());
+        assertEquals("toString()", expectedTS4, ts4.toString());
+    }
+
 }
index 5029a3b923039f27db812e115e777bd14488b4e3..242762bb865ceb575cd0b04e53bf3e15df31a984 100644 (file)
@@ -1,3 +1,7 @@
+2010-12-15  Francois Chouinard  <fchouinard@gmail.com>
+
+       * src/org/eclipse/linuxtools/lttng/event/LttngTimestamp.java: Fixed a rounding error 
+
 2010-12-13  Bernd Hufmann  <bhufmann@gmail.com>
 
        * src/org/eclipse/linuxtools/lttng/LttngConstants.java: Added file header.
index d13208bb02018bacbb1e859afe4b085470a1f6c3..6d7419ee78b7f05bc6f91a858377243d98423bc3 100644 (file)
@@ -141,23 +141,15 @@ public class LttngTimestamp extends TmfTimestamp {
     @SuppressWarnings("nls")
        public String toString() {
 
-        // If we are dealing with units of seconds (or higher),
-        // use the plain formatter
-        if (fScale >= 0) {
-            Double value = fValue * Math.pow(10, fScale);
-            return value.toString();
-        }
-
-        // Define a format string
-        String format = String.format("%%1d.%%0%dd", -fScale);
+        StringBuilder sb = new StringBuilder(String.valueOf(fValue));
 
-        // And format the timestamp value
-        double scale = Math.pow(10, fScale);
-        long seconds = (long) (fValue * scale);
-        long fracts  = fValue - (long) ((double) seconds / scale); 
-        String result = String.format(format, seconds, fracts);
-
-        return result;
+        // Prepend the correct number of "0" so we can insert a "." at the right location
+        int nbZeroes = (-fScale) - sb.length() + 1;  
+        for (int i = 0; i < nbZeroes; i++) {
+            sb.insert(i, "0");
+        }
+        sb.insert(sb.length() + fScale, ".");
+        return sb.toString();
     }
 
     @Override
This page took 0.032834 seconds and 5 git commands to generate.