/* Ċ Ġ* ĠCopyright Ġ 2 0 2 0 ĠGoogle ĠLLC . Ċ Ġ* Ċ Ġ* ĠThis Ġcode Ġmay Ġonly Ġbe Ġused Ġunder Ġthe ĠBSD Ġstyle Ġlicense Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / LICENSE . txt Ċ Ġ* Ċ Ġ* ĠCode Ġdistributed Ġby ĠGoogle Ġas Ġpart Ġof Ġthis Ġproject Ġis Ġalso Ġsubject Ġto Ġan Ġadditional ĠIP Ġrights Ċ Ġ* Ġgrant Ġfound Ġat Ċ Ġ* Ġhttp :// polymer . github . io / PAT ENTS . txt Ċ Ġ*/ Ċ Ċ package Ġar cs . core . util . performance Ċ Ċ import Ġar cs . core . util . Running Statistics Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġkotlin . test . assert Fails With Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . junit . runners . JUnit 4 Ċ Ċ @ RunWith ( JUnit 4 :: class ) Ċ class ĠCounter Statistics Test Ġ{ ĊĠ Ġ@ Test ĊĠ Ġfun Ġconstructor _ var arg Names () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics (" foo ", Ġ" bar ", Ġ" baz ") ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot () ĊĠĠĠ ĠassertThat ( snapshot . statistics ). contains Exactly ( ĊĠĠĠĠĠ Ġ" foo ", ĠRunning Statistics . Snapshot (), ĊĠĠĠĠĠ Ġ" bar ", ĠRunning Statistics . Snapshot (), ĊĠĠĠĠĠ Ġ" baz ", ĠRunning Statistics . Snapshot () ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġconstructor _ set Names () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics ( set Of (" foo ", Ġ" bar ", Ġ" baz ")) ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot () ĊĠĠĠ ĠassertThat ( snapshot . statistics ). contains Exactly ( ĊĠĠĠĠĠ Ġ" foo ", ĠRunning Statistics . Snapshot (), ĊĠĠĠĠĠ Ġ" bar ", ĠRunning Statistics . Snapshot (), ĊĠĠĠĠĠ Ġ" baz ", ĠRunning Statistics . Snapshot () ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġconstructor _ from Snapshot () Ġ{ ĊĠĠĠ Ġval Ġsnapshot Ġ= ĠCounter Statistics . Snapshot ( ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" foo " Ġto ĠRunning Statistics . Snapshot ( 1 , Ġ 1 . 0 , Ġ 0 . 0 , Ġ 0 . 0 , Ġ 1 . 0 , Ġ 1 . 0 ), ĊĠĠĠĠĠĠĠ Ġ" bar " Ġto ĠRunning Statistics . Snapshot ( 1 , Ġ 2 . 0 , Ġ 0 . 0 , Ġ 0 . 0 , Ġ 2 . 0 , Ġ 2 . 0 ), ĊĠĠĠĠĠĠĠ Ġ" baz " Ġto ĠRunning Statistics . Snapshot ( 1 , Ġ 3 . 0 , Ġ 0 . 0 , Ġ 0 . 0 , Ġ 3 . 0 , Ġ 3 . 0 ) ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics ( snapshot ) ĊĠĠĠ ĠassertThat ( stats . snapshot ()). isEqualTo ( snapshot ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġappend () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics (" foo ") ĊĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters () Ġ// ĠFoo Ġnot Ġincrement ed ĊĠĠĠ Ġ) ĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot ()[" foo "] ĊĠĠĠ ĠassertThat ( snapshot . measure ments ). isEqualTo ( 4 ) ĊĠĠĠ ĠassertThat ( snapshot . min ). isEqualTo ( 0 . 0 ) ĊĠĠĠ ĠassertThat ( snapshot . max ). isEqualTo ( 3 . 0 ) ĊĠĠĠ ĠassertThat ( snapshot . mean ) ĊĠĠĠĠĠ Ġ. is Within ( 0 . 0 1 ). of ( 1 . 5 ) ĊĠĠĠ ĠassertThat ( snapshot . variance ) ĊĠĠĠĠĠ Ġ. is Within ( 0 . 0 1 ). of ( 1 . 2 5 ) ĊĠĠĠ ĠassertThat ( snapshot . standard De viation ) ĊĠĠĠĠĠ Ġ. is Within ( 0 . 0 1 ). of ( 1 . 1 2 ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsnapshot _ with Names _ creates Copy _ when Names Match () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics (" foo ", Ġ" bar ") ĊĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠĠĠ Ġit . increment (" bar ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot () ĊĠĠĠ Ġval Ġsnapshot Copy Ġ= Ġsnapshot . with Names ( set Of (" foo ", Ġ" bar ")) ĊĊĠĠĠ ĠassertThat ( snapshot Copy ). isEqualTo ( snapshot ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsnapshot _ with Extra Names _ creates Copy _ with Empty Values For Extras () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics (" foo ") ĊĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot () ĊĠĠĠ Ġval Ġsnapshot Copy Ġ= Ġsnapshot . with Names ( set Of (" foo ", Ġ" bar ")) ĊĊĠĠĠ ĠassertThat ( snapshot Copy ). isEqualTo ( ĊĠĠĠĠĠ ĠCounter Statistics . Snapshot ( ĊĠĠĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠĠĠ Ġ" foo " Ġto Ġsnapshot [" foo "], ĊĠĠĠĠĠĠĠĠĠ Ġ" bar " Ġto ĠRunning Statistics . Snapshot () ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġsnapshot _ with F ew er Names _ creates Copy _ with Missing Names Removed () Ġ{ ĊĠĠĠ Ġval Ġstats Ġ= ĠCounter Statistics (" foo ", Ġ" bar ") ĊĊĠĠĠ Ġstats . append ( ĊĠĠĠĠĠ Ġstats . create Counters (). also Ġ{ ĊĠĠĠĠĠĠĠ Ġit . increment (" foo ") ĊĠĠĠĠĠĠĠ Ġit . increment (" bar ") ĊĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġsnapshot Ġ= Ġstats . snapshot () ĊĠĠĠ Ġval Ġsnapshot Copy Ġ= Ġsnapshot . with Names ( set Of (" foo ")) ĊĊĠĠĠ Ġval Ġe Ġ= Ġassert Fails With < IllegalArgumentException > Ġ{ ĊĠĠĠĠĠ Ġsnapshot Copy [" bar "] ĊĠĠĠ Ġ} ĊĠĠĠ ĠassertThat ( e ). has Message That (). contains (" Counter Ġwith Ġname Ġ\" bar \" Ġnot Ġregistered ") ĊĊĠĠĠ ĠassertThat ( snapshot Copy [" foo "]). isEqualTo ( snapshot [" foo "]) ĊĠ Ġ} Ċ } Ċ